Covid-19 Data Visualization

Note: Click the following button to show or hide the code.

In [1]:
from IPython.display import HTML

HTML('''<script>
code_show=true; 
function code_toggle() {
 if (code_show){
 $('div.input').hide();
 } else {
 $('div.input').show();
 }
 code_show = !code_show
} 
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Show/Hide Code"></form>''')
Out[1]:

Introduction

Coronavirus, which has affected all our lives in unimaginable ways, needs no introduction to anyone. Covid-19 has changed the way we interact, our habits, and even our minds. It has brought all of us down on our knees and forced us to make ourselves quarantined in our homes. I took this quarantine time as a learning opportunity and thought of building a data visualization platform which helps telling the story of how this virus has been unfolding since it has started. There are already few similar platforms out there but here I have tried to make it more elaborate and present some additional useful plots and insights.

Objective

To build an interactive data visualization platform which makes it intuitive and easier to track different kinds of trends and statistics

Dataset

The dataset relating to cases are taken from the Johns Hopkins Covid-19 dataset, and the dataset relating to tests are from Our World in Data COVID-19 dataset.

In [2]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import ticker 
import pycountry_convert as pc
import folium, os
from datetime import datetime,date
from scipy.interpolate import make_interp_spline, BSpline
import plotly.express as px
from plotly.offline import init_notebook_mode, iplot 
import plotly.graph_objs as go
import plotly.offline as py
from plotly.offline import download_plotlyjs,init_notebook_mode,plot,iplot
from plotly.subplots import make_subplots
from IPython.core.display import display, HTML, Markdown as md
from tabulate import tabulate
%matplotlib inline
import warnings
warnings.filterwarnings('ignore')
py.init_notebook_mode() #this is required to save plotly figures in offline mode
In [3]:
%%javascript
IPython.OutputArea.prototype._should_scroll = function(lines) {
    return false;
}
In [4]:
#%%capture
# this is used to not print any cell output.
#comment out first line to print the output (enable print statement).
df_confirmed = pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv')
df_deaths = pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv')
df_recovered = pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_recovered_global.csv')
test_df= pd.read_csv("https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/testing/covid-testing-all-observations.csv")
pop_df= pd.read_csv("../Data/Population_Data_Country.csv")
test_df['Date']= pd.to_datetime(test_df['Date'], format= "%Y/%m/%d")
df_confirmed.shape, df_deaths.shape, df_recovered.shape, test_df.shape, pop_df.shape;
In [5]:
last_updated_date= df_confirmed.columns[-1]
second_last_updated_date= df_confirmed.columns[-2]
# display(Markdown("Latest data is available till" +" " + last_updated_date))
display(md("__Last updated:__ {}".format(last_updated_date)))
last_updated_date,second_last_updated_date; #semicolon is being used to not print as cell output

Last updated: 6/29/20

Data Pre-processing & preparation

In [6]:
df_confirmed = df_confirmed.rename(columns={"Province/State":"state","Country/Region": "country"})
df_deaths = df_deaths.rename(columns={"Province/State":"state","Country/Region": "country"})
df_recovered = df_recovered.rename(columns={"Province/State":"state","Country/Region": "country"})
In [7]:
# Changing the conuntry names as required by pycountry_convert Lib
df_confirmed.loc[df_confirmed['country'] == "US", "country"] = "USA"
df_deaths.loc[df_deaths['country'] == "US", "country"] = "USA"
df_recovered.loc[df_recovered['country'] == "US", "country"] = "USA"

df_confirmed.loc[df_confirmed['country'] == 'Korea, South', "country"] = 'South Korea'
df_deaths.loc[df_deaths['country'] == 'Korea, South', "country"] = 'South Korea'
df_recovered.loc[df_recovered['country'] == 'Korea, South', "country"] = 'South Korea'

df_confirmed.loc[df_confirmed['country'] == 'Taiwan*', "country"] = 'Taiwan'
df_deaths.loc[df_deaths['country'] == 'Taiwan*', "country"] = 'Taiwan'
df_recovered.loc[df_recovered['country'] == 'Taiwan*', "country"] = 'Taiwan'

df_confirmed.loc[df_confirmed['country'] == 'Congo (Kinshasa)', "country"] = 'Democratic Republic of the Congo'
df_deaths.loc[df_deaths['country'] == 'Congo (Kinshasa)', "country"] = 'Democratic Republic of the Congo'
df_recovered.loc[df_recovered['country'] == 'Congo (Kinshasa)', "country"] = 'Democratic Republic of the Congo'

df_confirmed.loc[df_confirmed['country'] == "Cote d'Ivoire", "country"] = "Côte d'Ivoire"
df_deaths.loc[df_deaths['country'] == "Cote d'Ivoire", "country"] = "Côte d'Ivoire"
df_recovered.loc[df_recovered['country'] == "Cote d'Ivoire", "country"] = "Côte d'Ivoire"

df_confirmed.loc[df_confirmed['country'] == "Reunion", "country"] = "Réunion"
df_deaths.loc[df_deaths['country'] == "Reunion", "country"] = "Réunion"
df_recovered.loc[df_recovered['country'] == "Reunion", "country"] = "Réunion"

df_confirmed.loc[df_confirmed['country'] == 'Congo (Brazzaville)', "country"] = 'Republic of the Congo'
df_deaths.loc[df_deaths['country'] == 'Congo (Brazzaville)', "country"] = 'Republic of the Congo'
df_recovered.loc[df_recovered['country'] == 'Congo (Brazzaville)', "country"] = 'Republic of the Congo'

df_confirmed.loc[df_confirmed['country'] == 'Bahamas, The', "country"] = 'Bahamas'
df_deaths.loc[df_deaths['country'] == 'Bahamas, The', "country"] = 'Bahamas'
df_recovered.loc[df_recovered['country'] == 'Bahamas, The', "country"] = 'Bahamas'

df_confirmed.loc[df_confirmed['country'] == 'Gambia, The', "country"] = 'Gambia'
df_deaths.loc[df_deaths['country'] == 'Gambia, The', "country"] = 'Gambia'

df_recovered.loc[df_recovered['country'] == 'Gambia, The', "country"] = 'Gambia'
In [8]:
#%%capture
#only few countries has data relating multiple states/provinces.
df_confirmed["country"].value_counts(); #remove semicolon to print

As we see below, there are no missing values except in state column. Since, we are dealing on country level and not on state level, imputation is not needed.

In [9]:
#%%capture
display(md("__Number of missing values in confirmed cases data__:"))
print(df_confirmed.isna().sum()[df_confirmed.columns[df_confirmed.isna().sum()!=0].tolist()].to_string())
display(md("__Number of missing values in deaths data__:"))
print(df_deaths.isna().sum()[df_deaths.columns[df_deaths.isna().sum()!=0].tolist()].to_string())
display(md("__Number of missing values in recovered cases data__:"))
print(df_recovered.isna().sum()[df_recovered.columns[df_recovered.isna().sum()!=0].tolist()].to_string())
#replace all nans with empty string
df_confirmed = df_confirmed.replace(np.nan, '', regex=True)
df_deaths = df_deaths.replace(np.nan, '', regex=True)
df_recovered = df_recovered.replace(np.nan, '', regex=True)
#From 6th column we have numbers corrsponding to different dates.
# df_confirmed.iloc[:,5:].fillna(0, inplace=True)
# df_recovered.iloc[:,5:].fillna(0, inplace=True)
# df_deaths.iloc[:,5:].fillna(0, inplace=True)

Number of missing values in confirmed cases data:

state    185

Number of missing values in deaths data:

state    185

Number of missing values in recovered cases data:

state    186
In [10]:
#Inserting continent column
countries_confirmed = np.asarray(df_confirmed["country"])
countries_deaths = np.asarray(df_deaths["country"])
countries_recovered = np.asarray(df_recovered["country"])
# Continent_code to Continent_names
continents = {
    'NA': 'North America',
    'SA': 'South America', 
    'AS': 'Asia',
    'OC': 'Australia',
    'AF': 'Africa',
    'EU' : 'Europe',
    'na' : 'Others'
}

# Defininng Function for getting continent code for country.
def country_to_continent_code(country):
    try:
        return pc.country_alpha2_to_continent_code(pc.country_name_to_country_alpha2(country))
    except :
        return 'na'

#Collecting Continent Information
df_confirmed.insert(2,"continent", [continents[country_to_continent_code(country)] for country in countries_confirmed[:]])
df_deaths.insert(2,"continent",  [continents[country_to_continent_code(country)] for country in countries_deaths[:]])

df_recovered.insert(2,"continent",  [continents[country_to_continent_code(country)] for country in countries_recovered[:]] )
In [11]:
%%capture 
# this is used to not print any cell output.
#comment out first line to print the output (enable print statement).
# getting country wise data
#here we are sorting first, because we want the Lat and long for a country corresponding to its most affected (most cases on last updated date) state.
#for some countries there are multiple states, that is why it needs to be done
df_confirmed_sorted_total=  df_confirmed.sort_values(by= last_updated_date, ascending= False)
df_recovered_sorted_total=  df_recovered.sort_values(by= last_updated_date, ascending= False)
df_deaths_sorted_total=  df_deaths.sort_values(by= last_updated_date, ascending= False)

#Adding all the states numbers to get the numbers at country level
confirmed_cases_country_1 =  df_confirmed_sorted_total.groupby(["country"]).sum().drop(['Lat','Long'],axis =1).reset_index()
recovered_cases_country_1 = df_recovered_sorted_total.groupby(["country"]).sum().drop(['Lat','Long'],axis =1).reset_index()
deaths_country_1 = df_deaths_sorted_total.groupby(["country"]).sum().drop(['Lat','Long'],axis =1).reset_index()
# active_cases_country_1 = df_active.groupby(["country"]).sum().drop(['Lat','Long'],axis =1).reset_index()

#getting the Lat and Long corresponding to most affected state in a country.
#although it is better to get the Lat and Long info corresponding to the capital city. but for that we have to import another dataset.
confirmed_cases_country_2 =  df_confirmed_sorted_total.groupby(["country"])['Lat', 'Long'].first().reset_index()
recovered_cases_country_2 =  df_recovered_sorted_total.groupby(["country"])['Lat', 'Long'].first().reset_index()
deaths_country_2 =  df_deaths_sorted_total.groupby(["country"])['Lat', 'Long'].first().reset_index()
# active_cases_country_2 =  df_active.groupby(["country"])['Lat', 'Long'].mean().reset_index()

confirmed_cases_country= confirmed_cases_country_2.merge(confirmed_cases_country_1, how='inner', on='country')
recovered_cases_country= recovered_cases_country_2.merge(recovered_cases_country_1, how='inner', on='country')
deaths_country= deaths_country_2.merge(deaths_country_1, how='inner', on='country')
# active_cases_country= active_cases_country_2.merge(active_cases_country_1, how='inner', on='country')

#here again we sort but by country, because we want to find df for active cases by subtracting later
confirmed_cases_country= confirmed_cases_country.sort_values(by= "country", ascending= True)
recovered_cases_country= recovered_cases_country.sort_values(by= "country", ascending= True)
deaths_country= deaths_country.sort_values(by= "country", ascending= True)
# active_cases_country= active_cases_country.sort_values(by= "country", ascending= True)

# recovered_cases_country_1 = df_recovered.groupby(["country"]).sum().drop(['Lat','Long'],axis =1)
# deaths_country_1 = df_deaths.groupby(["country"]).sum().drop(['Lat','Long'],axis =1)
# active_cases_country_1 = df_active.groupby(["country"]).sum().drop(['Lat','Long'],axis =1)

# confirmed_cases.name = "Confirmed Cases"
# recovered_cases.name = "Recovered Cases"
# deaths.name = "Deaths Reported"
# active_cases.name = "Active Cases"
# df_countries_cases = pd.DataFrame([confirmed_cases,recovered_cases,deaths,active_cases]).transpose()

# getting continent wise data the same way we did for getting country level data
confirmed_cases_continent_1 =  df_confirmed_sorted_total.groupby(["continent"]).sum().drop(['Lat','Long'],axis =1).reset_index()
recovered_cases_continent_1 = df_recovered_sorted_total.groupby(["continent"]).sum().drop(['Lat','Long'],axis =1).reset_index()
deaths_continent_1 = df_deaths_sorted_total.groupby(["continent"]).sum().drop(['Lat','Long'],axis =1).reset_index()
# active_cases_continent_1 = df_active.groupby(["continent"]).sum().drop(['Lat','Long'],axis =1).reset_index()

confirmed_cases_continent_2 =  df_confirmed_sorted_total.groupby(["continent"])['Lat', 'Long'].first().reset_index()
recovered_cases_continent_2 =  df_recovered_sorted_total.groupby(["continent"])['Lat', 'Long'].first().reset_index()
deaths_continent_2 =  df_deaths_sorted_total.groupby(["continent"])['Lat', 'Long'].first().reset_index()
# active_cases_continent_2 =  df_active.groupby(["continent"])['Lat', 'Long'].mean().reset_index()

confirmed_cases_continent= confirmed_cases_continent_2.merge(confirmed_cases_continent_1, how='inner', on='continent')
recovered_cases_continent= recovered_cases_continent_2.merge(recovered_cases_continent_1, how='inner', on='continent')
deaths_continent= deaths_continent_2.merge(deaths_continent_1, how='inner', on='continent')
# active_cases_continent= active_cases_continent_2.merge(active_cases_continent_1, how='inner', on='continent')

confirmed_cases_continent= confirmed_cases_continent.sort_values(by= last_updated_date, ascending= False)
recovered_cases_continent= recovered_cases_continent.sort_values(by= last_updated_date, ascending= False)
deaths_continent= deaths_continent.sort_values(by= last_updated_date, ascending= False)
# active_cases_continent= active_cases_continent.sort_values(by= "continent", ascending= True)


# confirmed_cases.name = "Confirmed Cases"
# recovered_cases.name = "Recovered Cases"
# deaths.name = "Deaths Reported"
# active_cases.name = "Active Cases"
# df_continents_cases = pd.DataFrame([confirmed_cases,recovered_cases,deaths,active_cases]).transpose()
print(confirmed_cases_country.shape, recovered_cases_country.shape, deaths_country.shape);
print(confirmed_cases_continent.shape, recovered_cases_continent.shape, deaths_continent.shape);
In [12]:
%%capture
#Calculate active cases
active_cases_country = confirmed_cases_country.copy()
active_cases_country.iloc[:,3:] = (active_cases_country.iloc[:,3:] - recovered_cases_country.iloc[:,3:] - deaths_country.iloc[:,3:])#this makes all numeric columns to object type. that's why followin line is used to convert it back to numeric type.
active_cases_country.iloc[:,3:]= active_cases_country.iloc[:,3:].fillna(0)

active_cases_continent = confirmed_cases_continent.copy()
active_cases_continent.iloc[:,3:] = (active_cases_continent.iloc[:,3:] - recovered_cases_continent.iloc[:,3:] - deaths_continent.iloc[:,3:])#this makes all numeric columns to object type. that's why followin line is used to convert it back to numeric type.
active_cases_continent.iloc[:,3:]= active_cases_continent.iloc[:,3:].fillna(0)


# df_active= df_active.dropna(axis= 0, thresh= 68)
# df_active.iloc[:,5:]= df_active.iloc[:,5:].astype(int)
# df_active.iloc[:,5:]= df_active.iloc[:,5:].apply(lambda x: pd.to_numeric(x)) 
active_cases_country.shape, active_cases_continent.shape;
                           
In [13]:
#here we find sorted version of all these DataFrames in terms of numbers
confirmed_cases_country_sorted= confirmed_cases_country.sort_values(by= last_updated_date, ascending= False)
recovered_cases_country_sorted= recovered_cases_country.sort_values(by= last_updated_date, ascending= False)
deaths_country_sorted= deaths_country.sort_values(by= last_updated_date, ascending= False)
active_cases_country_sorted= active_cases_country.sort_values(by= last_updated_date, ascending= False)
In [14]:
#here we prepare data regarding tests
test_df["country"]= test_df["Entity"].str.split("-").apply(lambda x: x[0].strip(' '))
test_df_fil= test_df[['country', 'Date', 'Cumulative total', 'Daily change in cumulative total', \
                      'Cumulative total per thousand', 'Daily change in cumulative total per thousand']]

#There are more than one entries for a particular (country, date) combination. thats why we need to group and select the max one on a particular day
test_df_grp= test_df_fil.groupby(by=["country", "Date"])["Cumulative total", "Cumulative total per thousand"].max()
test_df_grp= test_df_grp.reset_index()
test_df_grp= test_df_grp.dropna(how="any")
test_df_grp= test_df_grp.sort_values(by= ["country", "Date"], ascending= True)

prev_country= test_df_grp.iloc[0,0]
curr_country= test_df_grp.iloc[0,0]
test_df_grp["New tests"]= 0
for i in range(1,test_df_grp.shape[0]):
    curr_country= test_df_grp.iloc[i,:]["country"]
    if(curr_country==prev_country):
        test_df_grp.iloc[i,-1]= test_df_grp.iloc[i,:]["Cumulative total"]-test_df_grp.iloc[i-1,:]["Cumulative total"]
    else:
        test_df_grp.iloc[i,-1]= 0
    prev_country= curr_country

    
#making the country name uniform across cases and test dataframes

test_df_grp.loc[test_df_grp["country"]== 'Czech Republic', "country"] = "Czechia"
test_df_grp.loc[test_df_grp["country"]== 'United States', "country"] = "USA"

#converting number of tests per thousand to tests per million
test_df_grp["Cumulative total per thousand"]= 1000*test_df_grp["Cumulative total per thousand"].astype(int)
# test_df_grp["Cumulative total per thousand"]= test_df_grp["Cumulative total per thousand"].astype(int)
test_df_grp= test_df_grp.rename(columns={"Cumulative total per thousand":"Total tests per million", "Cumulative total":"Total tests" })
country_list_test= np.unique(test_df_grp["country"])
n_countries_test= country_list_test.shape[0]
display(md("__Note:__ We have data relating to cases for __{}__ countries but data relating to tests are present for only __{}__ countries.".format(confirmed_cases_country.shape[0], n_countries_test)))
test_df_grp.shape;

Note: We have data relating to cases for 188 countries but data relating to tests are present for only 86 countries.

Data Visualization

Reported cases, deaths, and tests

In [15]:
#first merge country-wise and population data and then with test data
test_df_grp_country= test_df_grp.groupby(by="country")["Total tests", "Total tests per million"].max().reset_index()
confirmed_cases_pop= confirmed_cases_country.merge(pop_df, how="left", on= "country")
confirmed_cases_tests_pop= confirmed_cases_pop.merge(test_df_grp_country, how="left", on= "country")


table_stat_country= pd.DataFrame()
table_stat_country["Country"]= confirmed_cases_tests_pop["country"]
table_stat_country["Total cases"]= confirmed_cases_country[last_updated_date]
table_stat_country["New cases"]= confirmed_cases_country[last_updated_date]-confirmed_cases_country[second_last_updated_date]
table_stat_country["Total deaths"]= deaths_country[last_updated_date]
table_stat_country["New deaths"]= deaths_country[last_updated_date]- deaths_country[second_last_updated_date]
table_stat_country["Total active"]= active_cases_country[last_updated_date]
table_stat_country["Total recovered"]= recovered_cases_country[last_updated_date]

total_world_stat= table_stat_country.sum(axis=0)
total_world_stat["Country"]= "World"

table_stat_country["Total cases per million"]= round(confirmed_cases_country[last_updated_date]/(confirmed_cases_pop["population"]/1000000),2)
table_stat_country["Total deaths per million"]= round(deaths_country[last_updated_date]/(confirmed_cases_pop["population"]/1000000),2)
table_stat_country["Total tests"]= confirmed_cases_tests_pop["Total tests"]
table_stat_country["Total tests per million"]= confirmed_cases_tests_pop["Total tests per million"]

total_world_stat["Total cases per million"]= round(total_world_stat["Total cases"]/(confirmed_cases_pop["population"].sum()/1000000),2)
total_world_stat["Total deaths per million"]= round(total_world_stat["Total deaths"]/(confirmed_cases_pop["population"].sum()/1000000),2)

table_stat_country= table_stat_country.append(total_world_stat, ignore_index= True)
table_stat_country= table_stat_country.sort_values(by= "Total cases", ascending= False)
table_stat_country.reset_index(inplace= True, drop= True)
table_stat_country = table_stat_country.replace(np.nan, '', regex=True)
# try:
#     table_stat_country["Total cases per million "]= confirmed_cases_country[last_updated_date]/(confirmed_cases_pop["population"]/1000000)
#     table_stat_country["Total deaths per million "]= deaths_country[last_updated_date]/(confirmed_cases_pop["population"]/1000000)
# except:
#     table_stat_country["Total cases per million "]=''
#     table_stat_country["Total deaths per million "]=''

pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
# display(HTML(table_stat_country.to_html(index= False, justify= "center")))
display(HTML(table_stat_country.to_html( justify= "center")))
# table_stat_country.style.background_gradient(cmap='Reds', vmax= table_stat_country.iloc[1,1])
# print(tabulate(table_stat_country))
Country Total cases New cases Total deaths New deaths Total active Total recovered Total cases per million Total deaths per million Total tests Total tests per million
0 World 10302052 156261 505505 3612 4560734 5235813 1330.88 65.3
1 USA 2590552 41556 126140 337 1759209 705203 7826.38 381.08 3.22977e+07 97000
2 Brazil 1368195 24052 58314 692 552070 757811 6436.76 274.34 485000 2000
3 Russia 640246 6683 9152 92 228316 402778 4387.22 62.71 1.93344e+07 132000
4 India 566840 18522 16893 418 215125 334822 410.75 12.24 8.39836e+06 6000
5 United Kingdom 313470 830 43659 25 268443 1368 4617.59 643.12 4.85255e+06 71000
6 Peru 282365 2946 9504 187 101702 171159 8563.82 288.25 245315 7000
7 Chile 275999 4017 5575 66 34270 236154 14438 291.64 1.07964e+06 56000
8 Spain 248970 200 28346 3 70248 150376 5325.02 606.27 3.29039e+06 70000
9 Italy 240436 126 34744 6 16496 189196 3976.66 574.64 5.34184e+06 88000
10 Iran 225205 2536 10670 162 28355 186180 2681.24 127.03 1.61087e+06 19000
11 Mexico 220657 3805 27121 473 23389 170147 1711.41 210.35 480602 3000
12 Pakistan 209337 2825 4304 137 106530 98503 947.69 19.48 1.26216e+06 5000
13 France 201522 2046 29816 35 95582 76124 3087.35 456.79 831174 12000
14 Turkey 198613 1374 5115 18 21689 171809 2354.93 60.65 3.33116e+06 39000
15 Germany 195042 349 8976 8 8296 177770 2327.92 107.13 5.41266e+06 64000
16 Saudi Arabia 186436 3943 1599 48 57719 127118 5355.22 45.93 1.59114e+06 45000
17 South Africa 144264 6130 2529 73 71121 70614 2432.43 42.64 1.56708e+06 26000
18 Bangladesh 141801 4014 1783 45 82238 57780 861.02 10.83 733197 4000
19 Canada 105830 637 8628 46 28504 68698 2804.03 228.6 2.67663e+06 70000
20 Qatar 95106 693 113 3 14823 80170 33010.8 39.22 352659 122000
21 Colombia 91995 0 3256 0 50394 38345 1807.98 63.99 707688 13000
22 China 84780 23 4641 0 520 79619 58.9 3.22
23 Sweden 67667 2530 5310 30 62357 0 6700.19 525.78 444607 44000
24 Egypt 66754 1566 2872 83 45931 17951 652.31 28.06
25 Argentina 62268 2335 1280 48 39850 21138 1377.74 28.32 344409 7000
26 Belarus 61790 315 387 4 16190 45213 6539.09 40.96 992007 104000
27 Belgium 61361 66 9732 0 34688 16941 5294.48 839.72 929914 80000
28 Ecuador 55665 410 4502 73 23733 27430 3155.07 255.17 112845 6000
29 Indonesia 55092 1082 2805 51 28487 23800 201.42 10.26 465683 1000
30 Netherlands 50433 78 6126 2 44121 186 2943.3 357.52 581445 33000
31 United Arab Emirates 48246 449 314 1 10856 37076 4878.06 31.75
32 Iraq 47151 1749 1839 83 22338 22974 1172.25 45.72
33 Kuwait 45524 582 350 2 8861 36313 10659.9 81.96 382842 89000
34 Ukraine 44538 682 1161 19 23698 19679 1018.39 26.55 649150 14000
35 Singapore 43661 202 26 0 5650 37985 7462.98 4.44 684359 116000
36 Portugal 41912 266 1568 4 13139 27205 4110.35 153.78 1.10207e+06 108000
37 Oman 39060 910 169 6 16469 22422 7648.89 33.09
38 Philippines 36438 983 1255 11 25227 9956 332.52 11.45 636291 5000
39 Poland 34154 247 1444 6 11813 20897 902.43 38.15 1.35604e+06 35000
40 Panama 32785 1099 620 16 16570 15595 7598.32 143.69 114421 26000
41 Bolivia 32125 601 1071 57 22126 8928 2752.07 91.75 72236 6000
42 Dominican Republic 31816 443 733 7 13803 17280 2932.92 67.57
43 Switzerland 31652 35 1962 0 590 29100 3657.24 226.7 564747 65000
44 Afghanistan 31238 271 733 12 16571 13934 802.45 18.83
45 Romania 26582 269 1634 22 6036 18912 1381.77 84.94 694909 36000
46 Bahrain 26239 534 84 1 5227 20928 15420.4 49.37 536516 315000
47 Ireland 25462 23 1735 0 363 23364 5156.56 351.37 404989 82000
48 Nigeria 25133 566 573 8 15158 9402 121.92 2.78 132304 0
49 Armenia 25127 482 433 7 11397 13297 8479.56 146.12
50 Israel 24441 686 319 1 6904 17218 2823.74 36.86 864761 99000
51 Kazakhstan 21819 492 188 10 8623 13008 1162.02 10.01 1.40169e+06 74000
52 Honduras 18818 736 485 6 16372 1961 1899.92 48.97
53 Japan 18476 110 972 0 1116 16388 146.08 7.69 657245 5000
54 Austria 17723 69 703 1 600 16420 1967.82 78.06 606375 67000
55 Guatemala 17409 479 746 19 13493 3170 971.72 41.64
56 Ghana 17351 609 112 0 4245 12994 558.4 3.6 294867 9000
57 Azerbaijan 16968 544 206 8 7393 9369 1673.51 20.32
58 Moldova 16357 107 536 6 6592 9229 4054.82 132.87
59 Serbia 14288 242 274 4 1433 12581 1635.27 31.36 385713 56000
60 Algeria 13571 298 905 8 2992 9674 309.48 20.64
61 Nepal 13248 476 29 1 10085 3134 454.68 1 133377 4000
62 Denmark 12951 76 605 1 534 11812 2235.94 104.45 1.01887e+06 175000
63 South Korea 12800 43 282 0 981 11537 249.66 5.5 1.24095e+06 24000
64 Cameroon 12592 0 313 0 2179 10100 474.35 11.79
65 Morocco 12290 238 225 4 3232 8833 332.97 6.1 646195 17000
66 Czechia 11805 202 348 0 3711 7746 1102.35 32.5 545973 50000
67 Sudan 9257 0 572 0 4671 4014 211.11 13.04
68 Côte d'Ivoire 9214 113 66 0 5152 3996 349.3 2.5
69 Norway 8862 7 249 0 475 8138 1634.68 45.93 312008 57000
70 Malaysia 8637 3 121 0 182 8334 266.85 3.74 756171 23000
71 Uzbekistan 8222 274 23 1 2703 5496 245.66 0.69
72 Australia 7834 70 104 0 693 7037 307.22 4.08 2.37918e+06 93000
73 Finland 7209 11 328 0 281 6600 1301.09 59.2 239670 43000
74 Democratic Republic of the Congo 6939 112 167 10 5722 1050 77.48 1.86
75 Senegal 6698 112 108 3 2249 4341 400.03 6.45 78238 4000
76 North Macedonia 6209 129 298 12 3484 2427 2980.26 143.04
77 Kenya 6190 120 144 1 4033 2013 115.12 2.68 165196 3000
78 El Salvador 6173 239 164 12 2361 3648 951.71 25.28 157799 24000
79 Haiti 5933 156 105 5 4989 839 520.32 9.21
80 Tajikistan 5900 51 52 0 1342 4506 618.6 5.45
81 Ethiopia 5846 157 103 5 3313 2430 50.85 0.9 250604 2000
82 Venezuela 5530 233 48 4 3833 1649 194.47 1.69
83 Gabon 5394 185 42 2 2932 2420 2423.47 18.87
84 Guinea 5351 9 31 0 1024 4296 407.45 2.36
85 Kyrgyzstan 5296 279 57 7 2869 2370 811.75 8.74
86 Bulgaria 4831 140 223 4 2026 2582 695.26 32.09 133605 19000
87 Djibouti 4656 13 53 1 170 4433 4712.55 53.64
88 Bosnia and Herzegovina 4325 390 184 6 1775 2366 1318.27 56.08
89 Luxembourg 4256 14 110 0 149 3997 6798.96 175.73 177217 283000
90 Mauritania 4237 88 128 2 2612 1497 911.25 27.53
91 Hungary 4145 3 585 4 875 2685 429.07 60.56 273879 28000
92 Central African Republic 3613 184 47 2 2793 773 748.07 9.73
93 Greece 3390 14 191 0 1825 1374 325.24 18.32 308392 29000
94 Costa Rica 3269 139 15 0 1860 1394 641.72 2.94 28943 5000
95 Thailand 3169 7 58 0 58 3053 45.4 0.83 468000 6000
96 Somalia 2904 10 90 0 1904 910 182.72 5.66
97 Croatia 2725 34 107 0 463 2155 663.78 26.06 78183 19000
98 Kosovo 2677 508 49 12 1203 1425
99 Albania 2466 64 58 3 970 1438 856.91 20.15
100 Cuba 2340 8 86 0 43 2211 206.59 7.59 168545 14000
101 Maldives 2337 13 8 0 402 1927 4323.42 14.8 46182 85000
102 Paraguay 2191 64 16 1 1095 1080 307.18 2.24 65605 9000
103 West Bank and Gaza 2185 195 5 1 1733 447
104 Mali 2173 26 115 1 611 1447 107.3 5.68
105 Nicaragua 2170 0 74 0 858 1238 327.57 11.17
106 Madagascar 2138 60 20 2 1152 966 77.21 0.72
107 Sri Lanka 2039 2 11 0 350 1678 95.22 0.51
108 Equatorial Guinea 2001 0 32 0 1454 515 1426.24 22.81
109 South Sudan 1989 0 36 0 1707 246 177.69 3.22
110 Estonia 1987 0 69 0 100 1818 1497.89 52.02 106022 79000
111 Iceland 1840 2 10 0 12 1818 5392.05 29.3 63143 185000
112 Lithuania 1816 1 78 0 226 1512 667.09 28.65 418368 153000
113 Lebanon 1745 5 34 0 541 1170 255.66 4.98
114 Slovakia 1665 1 28 0 173 1464 304.97 5.13 208904 38000
115 Guinea-Bissau 1654 40 24 2 1313 317 840.45 12.2
116 Slovenia 1585 4 111 0 90 1384 762.41 53.39 98945 47000
117 Zambia 1568 11 22 0 235 1311 85.29 1.2
118 New Zealand 1528 0 22 0 22 1484 316.87 4.56 395510 82000
119 Sierra Leone 1450 23 60 0 429 961 181.77 7.52
120 Benin 1187 38 19 3 845 323 97.91 1.57
121 Tunisia 1172 3 50 0 93 1029 99.17 4.23 68104 5000
122 Cabo Verde 1165 10 12 0 545 608 2095.37 21.58
123 Malawi 1152 6 13 0 879 260 60.22 0.68
124 Jordan 1128 7 9 0 252 867 110.55 0.88
125 Yemen 1128 10 304 2 392 432 37.82 10.19
126 Latvia 1117 1 30 0 155 932 592.2 15.91 148777 78000
127 Republic of the Congo 1087 0 37 0 594 456 196.99 6.71
128 Niger 1075 1 67 0 69 939 44.41 2.77
129 Rwanda 1001 101 2 0 556 443 77.28 0.15 134749 10000
130 Cyprus 996 2 19 0 144 833 824.94 15.74
131 Burkina Faso 959 0 53 0 76 830 45.88 2.54
132 Uruguay 932 3 27 0 83 822 268.3 7.77 64183 18000
133 Georgia 926 2 15 0 120 791 232.13 3.76
134 Mozambique 883 24 6 1 648 229 28.25 0.19
135 Uganda 870 11 0 0 62 808 19.02 0 164225 3000
136 Chad 866 0 74 0 11 781 52.72 4.51
137 Andorra 855 0 52 0 4 799 11065.8 673.01
138 Libya 802 40 23 2 573 206 116.72 3.35
139 Eswatini 795 14 11 0 404 380 685.25 9.48
140 Liberia 770 2 36 2 422 312 152.24 7.12
141 Sao Tome and Principe 713 0 13 0 465 235
142 Diamond Princess 712 0 13 0 48 651
143 Jamaica 698 2 10 0 135 553 235.72 3.38
144 San Marino 698 0 42 0 0 656 20571.2 1237.81
145 Malta 670 0 9 0 22 639 1517.41 20.38
146 Togo 643 1 14 0 228 401 77.67 1.69
147 Zimbabwe 574 7 7 1 415 152 38.62 0.47 29641 1000
148 Tanzania 509 0 21 0 305 183 8.52 0.35
149 Montenegro 501 20 11 0 175 315 797.69 17.51
150 Suriname 501 11 13 2 271 217 854.03 22.16
151 Taiwan 447 0 7 0 5 435 18.77 0.29 75815 3000
152 Vietnam 355 0 0 0 20 335 3.65 0 261004 2000
153 Mauritius 341 0 10 0 5 326 268.13 7.86
154 Burma 299 0 6 0 72 221 5.5 0.11
155 Angola 276 9 11 0 172 93 8.4 0.33
156 Comoros 272 0 7 0 104 161 312.79 8.05
157 Syria 269 13 9 0 158 102 15.37 0.51
158 Guyana 235 5 12 0 109 114 298.77 15.26
159 Mongolia 220 0 0 0 45 175 67.11 0
160 Namibia 196 13 0 0 172 24 77.14 0
161 Eritrea 191 0 0 0 138 53 53.86 0
162 Botswana 175 83 1 0 149 25 74.42 0.43
163 Burundi 170 0 1 0 54 115 14.3 0.08
164 Cambodia 141 0 0 0 11 130 8.43 0
165 Brunei 141 0 3 0 0 138 322.3 6.86
166 Trinidad and Tobago 126 0 8 0 9 109 90.03 5.72
167 Bahamas 104 0 11 0 6 87 264.47 27.97
168 Monaco 103 0 4 0 4 95 2624.74 101.93
169 Barbados 97 0 7 0 0 90 337.54 24.36
170 Liechtenstein 82 0 1 0 0 81 2150.65 26.23
171 Bhutan 77 1 0 0 33 44 99.79 0
172 Seychelles 77 7 0 0 66 11 782.94 0
173 Antigua and Barbuda 69 0 3 0 44 22 704.59 30.63
174 Gambia 47 2 2 0 19 26 19.45 0.83
175 Saint Vincent and the Grenadines 29 0 0 0 0 29 261.4 0
176 Lesotho 27 0 0 0 23 4 12.6 0
177 Belize 24 0 2 0 4 18 60.36 5.03
178 Timor-Leste 24 0 0 0 0 24 18.2 0
179 Grenada 23 0 0 0 0 23 204.4 0
180 Saint Lucia 19 0 0 0 0 19 103.47 0
181 Laos 19 0 0 0 0 19 2.61 0
182 Dominica 18 0 0 0 0 18 250.05 0
183 Fiji 18 0 0 0 0 18 20.08 0
184 Saint Kitts and Nevis 15 0 0 0 0 15 281.96 0
185 Holy See 12 0 0 0 0 12 14981.3 0
186 Papua New Guinea 11 0 0 0 3 8 1.23 0
187 Western Sahara 10 0 1 0 1 8 16.74 1.67
188 MS Zaandam 9 0 2 0 7 0

Global distribution of cases

In [16]:
confirmed_cases_country_total= confirmed_cases_country.iloc[:,-1].sum()
recovered_cases_country_total= recovered_cases_country.iloc[:,-1].sum()
deaths_country_total= deaths_country.iloc[:,-1].sum()
active_cases_country_total= active_cases_country.iloc[:,-1].sum()
country_total_df= pd.DataFrame({'Confirmed cases': [confirmed_cases_country_total], 'Recovered cases':[recovered_cases_country_total], \
                               'Deaths Reported': [deaths_country_total], "Active Cases":  [active_cases_country_total]})
country_total_df_transpose= country_total_df.transpose().reset_index()
country_total_df_transpose.columns= ["Case type", "Number of cases"]
# country_total_df_transpose

fig = make_subplots(rows=1, cols=2,
                   specs=[[{"type": "xy"},{"type": "domain"}]])


trace1 = go.Bar(
                x = country_total_df_transpose['Case type'],
                y = country_total_df_transpose["Number of cases"],
                name = "Bar plot",
                marker = dict(color = ['#4D9DE0', '#3DB876', '#E15554','#E6C74C'], opacity= 0.7,
                             line=dict(color='rgb(0,0,0)',width=1.5)),
                text = country_total_df_transpose["Number of cases"],
                textposition='outside', hoverinfo= "x+y", showlegend= False)

trace2= go.Pie(
               values= country_total_df_transpose.iloc[1:,:]["Number of cases"],
               labels= country_total_df_transpose.iloc[1:,:]['Case type'],
               name= "",
               domain=dict(x=[0.5, 1.0], y= [0.2, 0.8]),
               textinfo='percent', textfont_size=15,
               hoverinfo= "label+value",
               hole=  .4, opacity= 0.9,
               marker_colors=['#3DB876', '#E15554','#E6C74C'],
#                marker = dict(line=dict(color='rgb(0,0,0)',width=1.5)), 
               textposition='outside')

fig.add_trace(trace1, row=1, col=1)
fig.add_trace(trace2, row=1, col=2)

fig.update_yaxes(title_text="<b>Number of cases</b>", row=1, col=1)

# fig.update_traces(marker_color='#46cdcf', opacity=0.8, textposition='outside')

fig.update_layout(width=1200, height= 500, title_text= '<b>Global distribution of cases</b>',
                 title_x=0.4, title_y= 0.9, plot_bgcolor='rgba(240,240,240,0.8)')
iplot(fig)

Number of countries affected over time

In [17]:
confirmed_cases_country_bool= confirmed_cases_country.drop(['Lat', 'Long'], axis=1)
# confirmed_cases_country_bool.iloc[:,1:]= confirmed_cases_country_bool.iloc[:,1:][confirmed_cases_country_bool.iloc[:,1:]!=0.0]
confirmed_cases_country_bool.set_index("country", inplace= True)
confirmed_cases_country_bool= confirmed_cases_country_bool!=0
n_country_confirmed_cases= confirmed_cases_country_bool.sum(axis=0)
n_country_confirmed_cases= n_country_confirmed_cases.reset_index()
n_country_confirmed_cases.columns= ['Date', 'Number of countries affected']

trace1 = go.Scatter(
                    x = n_country_confirmed_cases["Date"],
                    y = n_country_confirmed_cases["Number of countries affected"],
                    mode = "lines+markers",
                    name = "",
                    marker = dict(color = 'rgba(70, 174, 160, 1)')
#                     text= n_country_confirmed_cases["Number of countries affected"]
                    )
data = [trace1]
layout = dict(title = '<b>Number of countries affected over time</b>',
              xaxis= dict(title= '<b>Date</b>', tickangle= -45), yaxis= dict(title= '<b>Number of countries</b>'),
              title_x=0.5, title_y= 0.9, plot_bgcolor='rgba(240,240,240,0.8)'
             )
fig = dict(data = data, layout = layout)
iplot(fig)

# width=1200, height= 1000, title_text= 'Distribution of all continents till ' + last_updated_date,
#                  plot_bgcolor='rgb(250, 242, 242)'

Global COVID-19 trend

In [18]:
confirmed_cases_country_sum= confirmed_cases_country.drop(['Lat', 'Long'], axis=1)
confirmed_cases_country_sum.set_index("country", inplace= True)
confirmed_cases_country_sum= confirmed_cases_country_sum.sum(axis=0)
confirmed_cases_country_sum= confirmed_cases_country_sum.reset_index()
confirmed_cases_country_sum.columns= ['Date', 'Number of confirmed cases']

deaths_country_sum= deaths_country.drop(['Lat', 'Long'], axis=1)
deaths_country_sum.set_index("country", inplace= True)
deaths_country_sum= deaths_country_sum.sum(axis=0)
deaths_country_sum= deaths_country_sum.reset_index()
deaths_country_sum.columns= ['Date', 'Number of deaths']

active_cases_country_sum= active_cases_country.drop(['Lat', 'Long'], axis=1)
active_cases_country_sum.set_index("country", inplace= True)
active_cases_country_sum= active_cases_country_sum.sum(axis=0)
active_cases_country_sum= active_cases_country_sum.reset_index()
active_cases_country_sum.columns= ['Date', 'Number of active cases']

recovered_cases_country_sum= recovered_cases_country.drop(['Lat', 'Long'], axis=1)
recovered_cases_country_sum.set_index("country", inplace= True)
recovered_cases_country_sum= recovered_cases_country_sum.sum(axis=0)
recovered_cases_country_sum= recovered_cases_country_sum.reset_index()
recovered_cases_country_sum.columns= ['Date', 'Number of recovered cases']


fig = make_subplots(rows=2, cols=2, subplot_titles=("<b>Confirmed Cases</b>", "<b>Deaths Reported</b>", "<b>Active cases</b>", "<b>Recovered Cases</b>"),
                    vertical_spacing=0.12)
# create trace1 
trace1 = go.Scatter(
                x = confirmed_cases_country_sum['Date'],
                y = confirmed_cases_country_sum["Number of confirmed cases"],
                name = "Confirmed Cases",
                marker = dict(color = '#4D9DE0', opacity= 0.9),
                mode= 'lines+markers', hoverinfo='x+y'
                )
# create trace2 
trace2 = go.Scatter(
                x = deaths_country_sum['Date'],
                y = deaths_country_sum["Number of deaths"],
                name = "Deaths Reported",
                marker = dict(color = '#E15554', opacity=0.9 ),
                mode='lines+markers', hoverinfo= 'x+y')

trace3 = go.Scatter(
                x = active_cases_country_sum['Date'],
                y = active_cases_country_sum["Number of active cases"],
                name = "Active Cases",
                marker = dict(color = '#E6C74C', opacity=0.9),
                mode='lines+markers', hoverinfo= 'x+y')

trace4 = go.Scatter(
                x = recovered_cases_country_sum['Date'],
                y = recovered_cases_country_sum["Number of recovered cases"],
                name = "Recovered Cases",
                marker = dict(color = '#3DB876', opacity= 1),
                mode='lines+markers', hoverinfo= 'x+y')

fig.add_trace(trace1, row=1, col=1)
fig.add_trace(trace2, row=1, col=2)
fig.add_trace(trace3, row=2, col=1)
fig.add_trace(trace4, row=2, col=2)

# Update xaxis properties
# fig.update_xaxes(title_text="Country", row=1, col=1)
# fig.update_xaxes(title_text="Country", row=1, col=2)
fig.update_xaxes(title_text="<b>Date</b>", row=2, col=1, tickangle= -45)
fig.update_xaxes(title_text="<b>Date</b>", row=2, col=2, tickangle= -45)
fig.update_xaxes(tickangle= -45, row=1, col=1)
fig.update_xaxes(tickangle= -45, row=1, col=2)

# Update yaxis properties
fig.update_yaxes(title_text="<b>Number of cases</b>", row=1, col=1)
fig.update_yaxes(title_text="", row=1, col=2)
fig.update_yaxes(title_text="<b>Number of cases</b>", row=2, col=1)
fig.update_yaxes(title_text="", row=2, col=2)



# data = [trace1, trace2, trace3, trace4]
fig.update_layout(width=1200, height= 1000, title_text= '<b>Global COVID-19 trend</b>',
                 title_x=0.45, title_y= 0.97, plot_bgcolor='rgba(240,240,240,0.8)')
# fig = go.Figure(data = data, layout = layout)
iplot(fig)

Continent-wise distrbution

In [19]:
confirmed_cases_continent_sorted= confirmed_cases_continent.sort_values(by= last_updated_date, ascending= False).iloc[:,[0,-1]]
recovered_cases_continent_sorted= recovered_cases_continent.sort_values(by= last_updated_date, ascending= False).iloc[:, [0,-1]]
deaths_continent_sorted= deaths_continent.sort_values(by= last_updated_date, ascending= False).iloc[:, [0,-1]]
active_cases_continent_sorted= active_cases_continent.sort_values(by= last_updated_date, ascending= False).iloc[:,[0,-1]]

fig = make_subplots(rows=2, cols=2, subplot_titles=("<b>Confirmed Cases</b>", "<b>Deaths Reported</b>", "<b>Active Cases</b>", "<b>Recovered Cases</b>"),
                    vertical_spacing=0.13)
# create trace1 
trace1 = go.Bar(
                x = confirmed_cases_continent_sorted['continent'],
                y = confirmed_cases_continent_sorted[last_updated_date],
                name = "Confirmed Cases",
                marker = dict(color = 'rgba(80, 26, 80, 0.3)', opacity= 1,
                             line=dict(color='rgb(0,0,0)',width=1.5)),
                text = confirmed_cases_continent_sorted[last_updated_date],
                textposition='outside', hoverinfo= 'x+y')

# create trace2
trace2 = go.Bar(
                x = deaths_continent_sorted['continent'],
                y = deaths_continent_sorted[last_updated_date],
                name = "Deaths Reported",
                marker = dict(color = 'rgba(70, 174, 160, 0.3)', opacity=1,
                              line=dict(color='rgb(0,0,0)',width=1.5)),
                text = deaths_continent_sorted[last_updated_date],
                textposition='outside', hoverinfo= 'x+y')

trace3 = go.Bar(
                x = active_cases_continent_sorted['continent'],
                y = active_cases_continent_sorted[last_updated_date],
                name = "Active Cases",
                marker = dict(color = 'rgba(130, 109, 186, 0.3)', opacity= 1,
                              line=dict(color='rgb(0,0,0)',width=1.5)),
                text = active_cases_continent_sorted[last_updated_date],
                textposition='outside', hoverinfo= 'x+y')


trace4 = go.Bar(
                x = recovered_cases_continent_sorted['continent'],
                y = recovered_cases_continent_sorted[last_updated_date],
                name = "Recovered Cases",
                marker = dict(color = '#46cdcf', opacity= 0.5,
                              line=dict(color='rgb(0,0,0)',width=1.5)),
                text = recovered_cases_continent_sorted[last_updated_date],
                textposition='outside', hoverinfo= 'x+y')


fig.add_trace(trace1, row=1, col=1)
fig.add_trace(trace2, row=1, col=2)
fig.add_trace(trace3, row=2, col=1)
fig.add_trace(trace4, row=2, col=2)

# Update xaxis properties
# fig.update_xaxes(title_text="Continent", row=1, col=1)
# fig.update_xaxes(title_text="Continent", row=1, col=2)
fig.update_xaxes(title_text="", row=2, col=1)
fig.update_xaxes(title_text="", row=2, col=2)

# Update yaxis properties
fig.update_yaxes(title_text="<b>Number of cases</b>", row=1, col=1)
# fig.update_yaxes(title_text="Number of cases", row=1, col=2)
fig.update_yaxes(title_text="<b>Number of cases</b>", row=2, col=1)
# fig.update_yaxes(title_text="Number of cases", row=2, col=2)




# data = [trace1, trace2, trace3, trace4]
fig.update_layout(barmode = "group", width=1100, height= 1000, title_text= '<b>Continent-wise distribution</b>',
                 title_x=0.45, title_y= 0.97, plot_bgcolor='rgba(240,240,240,0.8)')
# fig = go.Figure(data = data, layout = layout)
iplot(fig)

Continent-wise distribution - Pie chart

In [20]:
n_rows, n_cols= 3,3
fig = make_subplots(rows=n_rows, cols=n_cols, subplot_titles=(confirmed_cases_continent_sorted["continent"].tolist()),
                    vertical_spacing=0.1, 
                    specs=[[{"type": "pie"}]*n_cols]*n_rows)
cnt= 1
for row in range(n_rows):
    for col in range(n_cols):
        if (cnt>confirmed_cases_continent_sorted.shape[0]): break
        continent_name= confirmed_cases_continent_sorted.iloc[row*(n_cols)+col,:]["continent"]
#         print(continent_name, row*(n_cols)+col)
        n_confirmed= confirmed_cases_continent_sorted[confirmed_cases_continent_sorted["continent"]==continent_name][last_updated_date].values
        n_recovered= recovered_cases_continent_sorted[recovered_cases_continent_sorted["continent"]==continent_name][last_updated_date].values
        n_deaths= deaths_continent_sorted[deaths_continent_sorted["continent"]==continent_name][last_updated_date].values
        n_active= active_cases_continent_sorted[active_cases_continent_sorted["continent"]==continent_name][last_updated_date].values
        
        continent_dist_df= pd.DataFrame({'Confirmed Cases': n_confirmed, 'Recovered Cases':n_recovered, \
                               'Deaths Reported': n_deaths, "Active Cases":  n_active})
        continent_dist_df_transpose= continent_dist_df.transpose().reset_index()
        continent_dist_df_transpose.columns= ["Case type", "Number of cases"]
        trace= go.Pie(
               values= continent_dist_df_transpose.iloc[1:,:]["Number of cases"],
               labels= continent_dist_df_transpose.iloc[1:,:]['Case type'],
               name= continent_name,
#                domain=dict(x=[0.5, 1.0], y= [0.2, 0.8]),
               textinfo='percent', textfont_size=10,
               hoverinfo= "label+value",
               hole=  .4, opacity= 0.8,
               marker_colors=['#3DB876', '#E15554','#E6C74C'],
#                marker = dict(line=dict(color='rgb(0,0,0)',width=1.5)), 
               textposition='outside')
        fig.add_trace(trace, row= row+1, col= col+1)
        cnt+=1

fig.update_layout(width=1200, height= 1000, title_text= '<b>Continent-wise distribution</b>',
                 title_x=0.45, title_y= 0.97, plot_bgcolor='rgba(240,240,240,0.8)')
iplot(fig)

Distribution of confirmed cases and deaths - Top 20 countries

In [21]:
table_stat_country_death_sorted= table_stat_country.sort_values(by="Total deaths", ascending= False)

fig = make_subplots(rows=1, cols=2, subplot_titles= ("<b>Confirmed Cases", "<b>Deaths Reported</b>"),
                   horizontal_spacing=0.1, specs=[[{"type": "domain"},{"type": "domain"}]])


trace1= go.Pie(
               values= table_stat_country.iloc[1:21,:]["Total cases"],
               labels= table_stat_country.iloc[1:21,:]['Country'],
               name= "",
               domain=dict(x=[0.0, 0.5], y= [0.2, 0.8]),
               textinfo='percent', textfont_size=10,
               hoverinfo= "label+value",
               hole=  .4, opacity= 0.9,
#                marker_colors=['rgba(0,175,0,0.7)', 'rgba(255,0,0,0.8)','rgba(0,0,255,0.7)'],
#                marker = dict(line=dict(color='rgb(0,0,0)',width=1.5)), 
               textposition='outside')

trace2= go.Pie(
               values= table_stat_country_death_sorted.iloc[1:21,:]["Total deaths"],
               labels= table_stat_country_death_sorted.iloc[1:21,:]['Country'],
               name= "",
               domain=dict(x=[0.5, 1.0], y= [0.2, 0.8]),
               textinfo='percent', textfont_size=10,
               hoverinfo= "label+value",
               hole=  .4, opacity= 0.9,
#                marker_colors=['rgba(0,175,0,0.7)', 'rgba(255,0,0,0.8)','rgba(0,0,255,0.7)'],
#                marker = dict(line=dict(color='rgb(0,0,0)',width=1.5)), 
               textposition='outside')

fig.add_trace(trace1, row=1, col=1)
fig.add_trace(trace2, row=1, col=2)

# fig.update_yaxes(title_text="Number of cases", row=1, col=1)

# fig.update_traces(marker_color='#46cdcf', opacity=0.8, textposition='outside')

fig.update_layout(width=1100, height= 500, title_text= '<b>Distribution among top 20 countries</b>',
                 title_x=0.45, title_y= 0.97, plot_bgcolor='rgba(240,240,240,0.8)')
iplot(fig)

Distribution of cases - Top 10 countries - Bar chart

In [22]:
confirmed_cases_country_top_10= confirmed_cases_country.sort_values(by= last_updated_date, ascending= False).iloc[:10,[0,-1]]
recovered_cases_country_top_10= recovered_cases_country[recovered_cases_country['country'].isin(confirmed_cases_country_top_10['country'])]
deaths_country_top_10= deaths_country[deaths_country['country'].isin(confirmed_cases_country_top_10['country'])]
active_cases_country_top_10= active_cases_country[active_cases_country['country'].isin(confirmed_cases_country_top_10['country'])]

recovered_cases_country_top_10= recovered_cases_country_top_10.sort_values(by= last_updated_date, ascending= False).iloc[:, [0,-1]]
deaths_country_top_10= deaths_country_top_10.sort_values(by= last_updated_date, ascending= False).iloc[:, [0,-1]]
active_cases_country_top_10= active_cases_country_top_10.sort_values(by= last_updated_date, ascending= False).iloc[:,[0,-1]]

fig = make_subplots(rows=2, cols=2, subplot_titles=("<b>Confirmed Cases</b>", "<b>Deaths Reported</b>", "<b>Active Cases</b>", "<b>Recovered Cases</b>"),
                    vertical_spacing=0.13)
# create trace1 
trace1 = go.Bar(
                x = confirmed_cases_country_top_10['country'],
                y = confirmed_cases_country_top_10[last_updated_date],
                name = "Confirmed cases",
                marker = dict(color = 'rgba(80, 26, 80, 0.3)', opacity= 0.7,
                             line=dict(color='rgb(0,0,0)',width=1.5)),
                text = confirmed_cases_country_top_10[last_updated_date],
                textposition='outside', hoverinfo= 'x+y')
# create trace2 
trace4 = go.Bar(
                x = recovered_cases_country_top_10['country'],
                y = recovered_cases_country_top_10[last_updated_date],
                name = "Recovered cases",
                marker = dict(color = '#46cdcf', opacity= 0.5,
                              line=dict(color='rgb(0,0,0)',width=1.5)),
                text = recovered_cases_country_top_10[last_updated_date],
                textposition='outside', hoverinfo= 'x+y')

trace2 = go.Bar(
                x = deaths_country_top_10['country'],
                y = deaths_country_top_10[last_updated_date],
                name = "Deaths Reported",
                marker = dict(color = 'rgba(70, 174, 160, 0.3)',
                              line=dict(color='rgb(0,0,0)',width=1.5)),
                text = deaths_country_top_10[last_updated_date],
                textposition='outside', hoverinfo= 'x+y')

trace3 = go.Bar(
                x = active_cases_country_top_10['country'],
                y = active_cases_country_top_10[last_updated_date],
                name = "Active Cases",
                marker = dict(color = 'rgba(130, 109, 186, 0.3)', opacity= 0.7,
                              line=dict(color='rgb(0,0,0)',width=1.5)),
                text = active_cases_country_top_10[last_updated_date],
                textposition='outside', hoverinfo= 'x+y')

fig.add_trace(trace1, row=1, col=1)
fig.add_trace(trace2, row=1, col=2)
fig.add_trace(trace3, row=2, col=1)
fig.add_trace(trace4, row=2, col=2)

# Update xaxis properties
# fig.update_xaxes(title_text="Country", row=1, col=1)
# fig.update_xaxes(title_text="Country", row=1, col=2)
fig.update_xaxes(title_text="", row=2, col=1)
fig.update_xaxes(title_text="", row=2, col=2)

# Update yaxis properties
fig.update_yaxes(title_text="<b>Number of cases</b>", row=1, col=1)
# fig.update_yaxes(title_text="Number of cases", row=1, col=2)
fig.update_yaxes(title_text="<b>Number of cases</b>", row=2, col=1)
# fig.update_yaxes(title_text="Number of cases", row=2, col=2)




# data = [trace1, trace2, trace3, trace4]
fig.update_layout(barmode = "group", width=1100, height= 1000, title_text= '<b>Distribution among top 10 countries</b>',
                 title_x=0.45, title_y= 0.97, plot_bgcolor='rgba(240,240,240,0.8)')
# fig = go.Figure(data = data, layout = layout)
iplot(fig)

Distribution of cases - Top 10 countries - Pie chart

In [23]:
n_rows, n_cols= 3,4
fig = make_subplots(rows=n_rows, cols=n_cols, subplot_titles=(confirmed_cases_country_top_10["country"].tolist()),
                    vertical_spacing=0.07, horizontal_spacing=0.1, 
                    specs=[[{"type": "pie"}]*n_cols]*n_rows)
cnt= 1
for row in range(n_rows):
    for col in range(n_cols):
        if (cnt>confirmed_cases_country_top_10.shape[0]): break
        country_name= confirmed_cases_country_top_10.iloc[row*(n_cols)+col,:]["country"]
#         print(country_name, row*(n_cols)+col)
        n_confirmed= confirmed_cases_country_top_10[confirmed_cases_country_top_10["country"]==country_name][last_updated_date].values
        n_recovered= recovered_cases_country_top_10[recovered_cases_country_top_10["country"]==country_name][last_updated_date].values
        n_deaths= deaths_country_top_10[deaths_country_top_10["country"]==country_name][last_updated_date].values
        n_active= active_cases_country_top_10[active_cases_country_top_10["country"]==country_name][last_updated_date].values
        
        country_dist_df= pd.DataFrame({'Confirmed cases': n_confirmed, 'Recovered cases':n_recovered, \
                               'Deaths Reported': n_deaths, "Active Cases":  n_active})
        country_dist_df_transpose= country_dist_df.transpose().reset_index()
        country_dist_df_transpose.columns= ["Case type", "Number of cases"]
#         country_dist_df_transpose= country_dist_df_transpose.iloc[1:,:] #COMMENT THIS LINE IF YOU WANT CONFIRMED CASES IN PIE CHART TOO
        trace= go.Pie(
               values= country_dist_df_transpose.iloc[1:,:]["Number of cases"],
               labels= country_dist_df_transpose.iloc[1:,:]['Case type'],
               name= 'Total confirmed cases: {}'.format(country_dist_df_transpose.iloc[0,1]),
#                domain=dict(x=[0.5, 1.0], y= [0.2, 0.8]),
               textinfo='percent', textfont_size=10,
               hoverinfo= "label+value",
               hole=  .4, opacity= 0.8, marker_colors=['#3DB876', '#E15554','#E6C74C' ],
#                marker = dict(line=dict(color='rgb(0,0,0)',width=1.5)), 
               textposition='outside')
        fig.add_trace(trace, row= row+1, col= col+1)
        cnt+=1

fig.update_layout(width=1100, height= 900, title_text= '<b>Distribution of top 10 countries</b>',
                 title_x=0.45, title_y= 0.97, plot_bgcolor='rgba(240,240,240,0.8)')
iplot(fig)

Bubble Map plot

In [36]:
# df= pd.read_csv("https://raw.githubusercontent.com/emmagrimaldi/Capstone_Predicting_Storm_Damages/master/all_storms.csv")
# countries= np.unique(df.EVENT_TYPE)[:7]

    
n_countries= confirmed_cases_country.shape[0]
mapbox_access_token = "pk.eyJ1IjoiYW5hdWliMTY1IiwiYSI6ImNrOGs5cjE2cDBnMXEzbGxzcXIybDRqMHQifQ.f7TquNmFYNZ0xR0cvT1Lrw"

data = []

#preparing data for the scattermap plot
for i in range(confirmed_cases_country_sorted.shape[0]):
    country_name= confirmed_cases_country_sorted.iloc[i,:]["country"]
    confirmed_cases_country_df= confirmed_cases_country_sorted.loc[confirmed_cases_country_sorted['country'] == country_name,:]
    deaths_country_df= deaths_country.loc[deaths_country['country'] == country_name,:]
    active_cases_country_df= active_cases_country.loc[active_cases_country['country'] == country_name,:]
    recovered_cases_country_df= recovered_cases_country.loc[recovered_cases_country['country'] == country_name,:]
    
    
    country_data = dict(
            lat = confirmed_cases_country_df['Lat'].values,
            lon = confirmed_cases_country_df['Long'].values,
            text = '<b>'+ confirmed_cases_country_df['country'].values[0] + \
                   ':<br><br><b>Confirmed: '+str(confirmed_cases_country_df[last_updated_date].values[0])+ \
                    ' (+' + str (confirmed_cases_country_df[last_updated_date].values[0] - confirmed_cases_country_df[second_last_updated_date].values[0]) +')' + \
                    '<br>Deaths: ' +str(deaths_country_df[last_updated_date].values[0])+ \
                    ' (+' + str (deaths_country_df[last_updated_date].values[0] - deaths_country_df[second_last_updated_date].values[0]) +')' + \
                    '<br>Active: ' +str(active_cases_country_df[last_updated_date].values[0])+ \
                    '<br>Recovered: '+str(recovered_cases_country_df[last_updated_date].values[0])+'</b>',
            hoverinfo= "text",
            name = country_name,
            marker = dict(
                #170 is the bubble size for topmost country and rest of the countries are scaled as per their confirmed cases
                size = (170*confirmed_cases_country_df[last_updated_date])/confirmed_cases_country_sorted.iloc[0,:][last_updated_date], 
                symbol= 'circle', sizemin= 2,
                color = "rgba(252, 146, 114, 0.8)",
#                 color= color_map[continent_name],
#                 line=dict(
#                 color='rgba(255,255,255,0.8)',
#                 width=1.5)
            ),
            type = 'scattermapbox'
        )
    data.append(country_data)


#basic layout info for the plot
layout = dict(
    height = 800,
    # top, bottom, left and right margins
    margin = dict(t = 0, b = 0, l = 0, r = 0),
    font = dict(color = '#FFFFFF', size = 11),
    paper_bgcolor = '#000000',
    showlegend=False,
    mapbox = dict(
        # here you need the token from Mapbox
        accesstoken = mapbox_access_token,
        bearing = 0, 
        # where we want the map to be centered
        center = dict(
            lat = 43,
            lon = 12
        ),
        # we want the map to be "parallel" to our screen, with no angle
        pitch = 0,
        # default level of zoom
        zoom = 1,
        # default map style
        style = 'dark'
    )
    )


# assigning the annotations to the layout
# layout['annotations'] = annotations

#designing dropdown menu for the layout of plot
list_updatemenus=list()

list_updatemenus_1_buttons= []
#adding button for World (all countries at once in map)

country_name= "All"
visible = [True] * n_countries
temp_dict = dict(label = country_name,
             method = 'update',
             args = [{'visible': visible},
                     {'title': 'Country: {}'.format(country_name)}])
list_updatemenus_1_buttons.append(temp_dict)
    

#adding buttons for all countries individually
for i in range(1,table_stat_country.shape[0]):
    country_name= table_stat_country.iloc[i,0]
    lat= confirmed_cases_country_sorted.loc[confirmed_cases_country_sorted["country"]==country_name, "Lat"].values[0]
    long= confirmed_cases_country_sorted.loc[confirmed_cases_country_sorted["country"]==country_name, "Long"].values[0]
    visible = [False] * n_countries
    visible[i-1] = True
    temp_dict = dict(label = country_name,
                 method = 'update',
                 args = [{'visible': visible}, 
                         {layout['mapbox']['center']['lat']: lat, layout['mapbox']['center']['lon']: long},
                         {'title': 'Country: {}'.format(country_name)}])
    list_updatemenus_1_buttons.append(temp_dict)
    
    
    
    # drop-down 2: select type of storm event to visualize
list_updatemenus_1 = dict(
         # for each button I specify which dictionaries of my data list I want to visualize. Remember I have 7 different
         # types of storms but I have 8 options: the first will show all of them, while from the second to the last option, only
         # one type at the time will be shown on the map
         buttons= list_updatemenus_1_buttons,
        type= 'dropdown',
        # direction where the drop-down expands when opened
        direction = 'down',
        # positional arguments
        x = 0.01,
        xanchor = 'left',
        y = 0.99,
        yanchor = 'bottom',
        # fonts and border
        bgcolor = '#EEEEEE',
        bordercolor = '#FFFFFF',
        font = dict(color = '#000000', size=14)
    )

list_updatemenus.append(list_updatemenus_1)
# assign the list of dictionaries to the layout dictionary
layout['updatemenus'] = list_updatemenus

figure = dict(data = data, layout = layout)
iplot(figure)

Country-wise trend of confirmed cases - Dropdown

In [25]:
n_countries= confirmed_cases_country.shape[0]
data = []

#Preparing data for World scatter plot
confirmed_cases_world= confirmed_cases_country.iloc[:,3:].sum(axis=0)
confirmed_cases_world_time= confirmed_cases_world.transpose().reset_index()
confirmed_cases_world_time.columns= ['Date', 'Number of confirmed cases']
country_trace = go.Scatter(
                x = confirmed_cases_world_time['Date'],
                y = confirmed_cases_world_time["Number of confirmed cases"],
                name = "World",
#                 marker = dict(color = '#46cdcf', opacity= 0.7, line=dict(color='rgb(0,0,0)',width=1.5)),
#                 text= '+'+ str (new_confirmed_cases_country_time["Number of new confirmed cases"]),
                text= "World"+ '<br><br>',
#                 textposition='outside',
                mode= 'lines+markers', hoverinfo= 'text+x+y'
                )

data.append(country_trace)


#preparing data for the scatter plot country-wise
for i in range(confirmed_cases_country_sorted.shape[0]):
    country_name= confirmed_cases_country_sorted.iloc[i,:]["country"]
    confirmed_cases_country_df= confirmed_cases_country_sorted.loc[confirmed_cases_country_sorted['country'] == country_name,:]
    confirmed_cases_country_time= confirmed_cases_country_df.transpose().reset_index().iloc[3:,:]
    confirmed_cases_country_time.columns= ['Date', 'Number of confirmed cases']
#     deaths_country_df= deaths_country.loc[deaths_country['country'] == country_name,:]
#     active_cases_country_df= active_cases_country.loc[active_cases_country['country'] == country_name,:]
#     recovered_cases_country_df= recovered_cases_country.loc[recovered_cases_country['country'] == country_name,:]
   
    country_trace = go.Scatter(
                x = confirmed_cases_country_time['Date'],
                y = confirmed_cases_country_time["Number of confirmed cases"],
                name = country_name,
                text= country_name+ '<br><br>',
#                 marker = dict(color = 'rgba(80, 26, 80, 0.8)', opacity= 1),
                mode= 'lines+markers', hoverinfo='text+x+y'
                )

    data.append(country_trace)


# assigning the annotations to the layout
# layout['annotations'] = annotations

#designing dropdown menu for the layout of plot

list_updatemenus_buttons= []
title_text= '<b>Confirmed Cases Trend</b>'
default_dict= dict(label = "All",
                 method = 'update',
                 args = [{'visible': [True]*(n_countries+1)},
                         {'title': title_text}])

list_updatemenus_buttons.append(default_dict)


title_text= '<b>Total confirmed cases: {}<br>Confirmed cases per million: {}</b>' \
                .format(table_stat_country.loc[table_stat_country["Country"]=="World", "Total cases"].values[0], \
                       table_stat_country.loc[table_stat_country["Country"]=="World", "Total cases per million"].values[0])

visible= [False]*(n_countries+1)
visible[0]= True
default_dict= dict(label = "World",
                 method = 'update',
                 args = [{'visible': visible},
                         {'title': title_text}])
list_updatemenus_buttons.append(default_dict)

for i in range(confirmed_cases_country_sorted.shape[0]):
    country_name= confirmed_cases_country_sorted.iloc[i,0]
    title_text= '<b>Total confirmed cases: {}<br>Confirmed cases per million: {}</b>' \
                .format(table_stat_country.loc[table_stat_country["Country"]==country_name, "Total cases"].values[0], \
                       table_stat_country.loc[table_stat_country["Country"]==country_name, "Total cases per million"].values[0])
    visible = [False] * (n_countries+1)
    visible[i+1] = True
    temp_dict = dict(label = country_name,
                 method = 'update',
                 args = [{'visible': visible},
                         {'title': title_text}, {'font':11}])
    list_updatemenus_buttons.append(temp_dict)
    
    
    
    # drop-down 2: select type of storm event to visualize
list_updatemenus = list( [dict(
         # for each button I specify which dictionaries of my data list I want to visualize. Remember I have 7 different
         # types of storms but I have 8 options: the first will show all of them, while from the second to the last option, only
         # one type at the time will be shown on the map
         buttons= list_updatemenus_buttons,
        type= 'dropdown',
        # direction where the drop-down expands when opened
        direction = 'down',
        # positional arguments
        x = -0.07,
        xanchor = 'right',
        y = 0.90,
        yanchor = 'bottom',
        # fonts and border
        bgcolor = '#EEEEEE',
        bordercolor = '#FFFFFF',
        font = dict(color = '#000000', size=14)
    )
])


#basic layout info for the plot
layout = dict(
    height = 600, width= 1200,
    plot_bgcolor='rgba(240,240,240,0.8)',
    title = '<b>Trend of confirmed cases</b>', title_x=0.55, title_y= 0.9,
    xaxis= dict(title= '<b>Date</b>', tickangle= -45), yaxis= dict(title= ''),
    # top, bottom, left and right margins
#     margin = dict(t = 0, b = 0, l = 0, r = 0),
    
    font = dict(color = 'rgba(0, 0, 0, 0.8)'),
#     paper_bgcolor = '#000000',
#     showlegend=False,
    
    )


# list_updatemenus.append(list_updatemenus_1)
# assign the list of dictionaries to the layout dictionary
layout['updatemenus'] = list_updatemenus

figure = dict(data = data, layout = layout)
iplot(figure)

New cases added over time - Dropdown

In [26]:
data = []
n_countries= confirmed_cases_country.shape[0]
new_confirmed_cases_country= confirmed_cases_country_sorted.copy()
for i in range(4, confirmed_cases_country_sorted.shape[1]):
    new_confirmed_cases_country.iloc[:,i]= confirmed_cases_country_sorted.iloc[:,i]-confirmed_cases_country_sorted.iloc[:,i-1]

#find number of days it is taking to get doubled for each country

#adding trace for World data for default dropdown selection
new_confirmed_cases_world= new_confirmed_cases_country.iloc[:,3:].sum(axis=0)
new_confirmed_cases_country_time= new_confirmed_cases_world.transpose().reset_index().iloc[3:,:]
new_confirmed_cases_country_time.columns= ['Date', 'Number of new confirmed cases']
country_trace = go.Scatter(
                x = new_confirmed_cases_country_time['Date'],
                y = new_confirmed_cases_country_time["Number of new confirmed cases"],
                name = "World",
#                 marker = dict(color = '#46cdcf', opacity= 0.7, line=dict(color='rgb(0,0,0)',width=1.5)),
#                 text= '+'+ str (new_confirmed_cases_country_time["Number of new confirmed cases"]),
                text= "World"+ '<br><br>',
#                 textposition='outside',
                mode= 'lines+markers', hoverinfo= 'text+x+y'
                )

data.append(country_trace)
    

#preparing data for the scatter plot country-wise
for i in range(confirmed_cases_country_sorted.shape[0]):
    country_name= confirmed_cases_country_sorted.iloc[i,:]["country"]
    new_confirmed_cases_country_df= new_confirmed_cases_country.loc[new_confirmed_cases_country['country'] == country_name,:]
    new_confirmed_cases_country_time= new_confirmed_cases_country_df.transpose().reset_index().iloc[3:,:]
    new_confirmed_cases_country_time.columns= ['Date', 'Number of new confirmed cases']
#     deaths_country_df= deaths_country.loc[deaths_country['country'] == country_name,:]
#     active_cases_country_df= active_cases_country.loc[active_cases_country['country'] == country_name,:]
#     recovered_cases_country_df= recovered_cases_country.loc[recovered_cases_country['country'] == country_name,:]
   
    country_trace = go.Scatter(
                x = new_confirmed_cases_country_time['Date'],
                y = new_confirmed_cases_country_time["Number of new confirmed cases"],
                name = country_name,
#                 marker = dict(color = '#46cdcf', opacity= 0.7, line=dict(color='rgb(0,0,0)',width=1.5)),
#                 text= '+'+ str (new_confirmed_cases_country_time["Number of new confirmed cases"]),
#                 textposition='outside',
                text= country_name+ '<br><br>',
                mode= "lines+markers", hoverinfo= 'text+x+y'
                )

    data.append(country_trace)


#designing dropdown menu for the layout of plot

list_updatemenus_buttons= []
title_text= '<b>Trend of new confirmed cases</b>'

visible = [True] * (n_countries+1)
default_dict= dict(label = "All",
                 method = 'update',
                 args = [{'visible': visible},
                         {'title': title_text}])
list_updatemenus_buttons.append(default_dict)


visible = [False] * (n_countries+1)
visible[0]= True
default_dict= dict(label = "World",
                 method = 'update',
                 args = [{'visible': visible},
                         {'title': title_text}])
list_updatemenus_buttons.append(default_dict)

for i in range(confirmed_cases_country_sorted.shape[0]):
    country_name= confirmed_cases_country_sorted.iloc[i,0]
    title_text= '<b>Trend of new confirmed cases</b>'
    
    visible = [False] * (n_countries+1)
    visible[i+1] = True
    temp_dict = dict(label = country_name,
                 method = 'update',
                 args = [{'visible': visible},
                         {'title': title_text}, {'font':11}])
    list_updatemenus_buttons.append(temp_dict)
    
    
    
    # drop-down 2: select type of storm event to visualize
list_updatemenus = list( [dict(
         # for each button I specify which dictionaries of my data list I want to visualize. Remember I have 7 different
         # types of storms but I have 8 options: the first will show all of them, while from the second to the last option, only
         # one type at the time will be shown on the map
         buttons= list_updatemenus_buttons,
        type= 'dropdown',
        # direction where the drop-down expands when opened
        direction = 'down',
        # positional arguments
        x = -0.09,
        xanchor = 'right',
        y = 0.90,
        yanchor = 'bottom',
        # fonts and border
        bgcolor = '#EEEEEE',
        bordercolor = '#FFFFFF',
        font = dict(color = '#000000', size=14)
    )
])


#basic layout info for the plot
layout = dict(
    height = 600, width= 1200,
    plot_bgcolor='rgba(240,240,240,0.8)',
    title = '<b>Trend of new confirmed cases</b>', title_x=0.55, title_y= 0.9,
    xaxis= dict(title= '<b>Date</b>', tickangle= -45), yaxis= dict(title= ''),
    # top, bottom, left and right margins
#     margin = dict(t = 0, b = 0, l = 0, r = 0),
    
    font = dict(color = 'rgba(0, 0, 0, 0.8)'),
#     paper_bgcolor = '#000000',
#     showlegend=False,
    
    )


# list_updatemenus.append(list_updatemenus_1)
# assign the list of dictionaries to the layout dictionary
layout['updatemenus'] = list_updatemenus

figure = dict(data = data, layout = layout)
iplot(figure)

Country-wise distribution - Pie chart

In [27]:
# df= pd.read_csv("https://raw.githubusercontent.com/emmagrimaldi/Capstone_Predicting_Storm_Damages/master/all_storms.csv")
# countries= np.unique(df.EVENT_TYPE)[:7]


n_countries= confirmed_cases_country.shape[0]
# confirmed_cases_country_sorted= confirmed_cases_country.sort_values(by= last_updated_date, ascending= False)

data = []

#adding trace for World data for default dropdown selection
country_name= table_stat_country.iloc[0,:]["Country"]
n_confirmed= table_stat_country[table_stat_country["Country"]==country_name]["Total cases"].values
n_recovered= table_stat_country[table_stat_country["Country"]==country_name]["Total recovered"].values
n_deaths= table_stat_country[table_stat_country["Country"]==country_name]["Total deaths"].values
n_active= table_stat_country[table_stat_country["Country"]==country_name]["Total active"].values
country_dist_df= pd.DataFrame({'Confirmed cases': n_confirmed, 'Recovered cases':n_recovered, \
                           'Deaths Reported': n_deaths, "Active Cases":  n_active})
country_dist_df_transpose= country_dist_df.transpose().reset_index()
country_dist_df_transpose.columns= ["Case type", "Number of cases"]

country_trace= go.Pie(
           values= country_dist_df_transpose.iloc[1:,:]["Number of cases"],
           labels= country_dist_df_transpose.iloc[1:,:]['Case type'],
           name= 'Total confirmed cases: {}'.format(country_dist_df_transpose.iloc[0,1]),
#                domain=dict(x=[0.5, 1.0], y= [0.2, 0.8]),
           textinfo='percent', textfont_size=10,
           hoverinfo= "label+value",
           hole=  .4, opacity= 0.8,
           marker_colors=['#3DB876', '#E15554','#E6C74C'], 
           textposition='outside')

data.append(country_trace)


#preparing data for the pie chart country-wise
for i in range(confirmed_cases_country_sorted.shape[0]):
    country_name= confirmed_cases_country_sorted.iloc[i,:]["country"]
    n_confirmed= confirmed_cases_country[confirmed_cases_country["country"]==country_name][last_updated_date].values
    n_recovered= recovered_cases_country[recovered_cases_country["country"]==country_name][last_updated_date].values
    n_deaths= deaths_country[deaths_country["country"]==country_name][last_updated_date].values
    n_active= active_cases_country[active_cases_country["country"]==country_name][last_updated_date].values
    country_dist_df= pd.DataFrame({'Confirmed cases': n_confirmed, 'Recovered cases':n_recovered, \
                               'Deaths Reported': n_deaths, "Active Cases":  n_active})
    country_dist_df_transpose= country_dist_df.transpose().reset_index()
    country_dist_df_transpose.columns= ["Case type", "Number of cases"]
   
    country_trace= go.Pie(
               values= country_dist_df_transpose.iloc[1:,:]["Number of cases"],
               labels= country_dist_df_transpose.iloc[1:,:]['Case type'],
               name= 'Total confirmed cases: {}'.format(country_dist_df_transpose.iloc[0,1]),
#                domain=dict(x=[0.5, 1.0], y= [0.2, 0.8]),
               textinfo='percent', textfont_size=10,
               hoverinfo= "label+value",
               hole=  .4, opacity= 0.8, visible=False,
               marker_colors=['#3DB876', '#E15554','#E6C74C'],
               textposition='outside')

    data.append(country_trace)


#designing dropdown menu for the layout of plot

list_updatemenus_buttons= []
title_text= '<b>Total confirmed cases: {}</b>' \
                .format(table_stat_country.loc[table_stat_country["Country"]=="World", "Total cases"].values[0])
visible = [False] * (n_countries+1)
visible[0]= True
default_dict= dict(label = "World",
                 method = 'update',
                 args = [{'visible': visible},
                         {'title': title_text}])

list_updatemenus_buttons.append(default_dict)
for i in range(confirmed_cases_country_sorted.shape[0]):
    country_name= confirmed_cases_country_sorted.iloc[i,0]
    title_text= '<b>Total confirmed cases: {}</b>' \
                .format(table_stat_country.loc[table_stat_country["Country"]==country_name, "Total cases"].values[0])
    visible = [False] * (n_countries+1)
    visible[i+1] = True
    temp_dict = dict(label = country_name,
                 method = 'update',
                 args = [{'visible': visible},
                         {'title': title_text}, {'font':11}])
    list_updatemenus_buttons.append(temp_dict)
    
    
    
    # drop-down 2: select type of storm event to visualize
list_updatemenus = list( [dict(
         # for each button I specify which dictionaries of my data list I want to visualize. Remember I have 7 different
         # types of storms but I have 8 options: the first will show all of them, while from the second to the last option, only
         # one type at the time will be shown on the map
         buttons= list_updatemenus_buttons,
        type= 'dropdown', active= 0,
        # direction where the drop-down expands when opened
        direction = 'down',
        # positional arguments
        x = -0.07,
        xanchor = 'right',
        y = 0.90,
        yanchor = 'bottom',
        # fonts and border
        bgcolor = '#EEEEEE',
        bordercolor = '#FFFFFF',
        font = dict(color = '#000000', size=14)
    )
])


#basic layout info for the plot
layout = dict(
    height = 600, width= 1000,
    plot_bgcolor='rgba(240,240,240,0.8)',
    title = '<b>Country-wise distribution</b>', title_x=0.55, title_y= 0.9,
    xaxis= dict(title= 'Date', tickangle= -45), yaxis= dict(title= 'Number of deaths reported'),
    # top, bottom, left and right margins
#     margin = dict(t = 0, b = 0, l = 0, r = 0),
    
    font = dict(color = 'rgba(0, 0, 0, 0.8)'),
#     paper_bgcolor = '#000000',
#     showlegend=False,
    
    )


# list_updatemenus.append(list_updatemenus_1)
# assign the list of dictionaries to the layout dictionary
layout['updatemenus'] = list_updatemenus

figure = dict(data = data, layout = layout)
iplot(figure)

Number of days to double - Confirmed cases

In [28]:
columns= confirmed_cases_country.columns
new_confirmed_cases_country_1= new_confirmed_cases_country.copy()
new_confirmed_cases_country_1["n_days_cases_double"]=0
for i in range(confirmed_cases_country_sorted.shape[0]):
    country_name= confirmed_cases_country_sorted.iloc[i,:]["country"]
#     print(country_name)
    n_days=1
    while(True):
        try:
            if(confirmed_cases_country.loc[confirmed_cases_country["country"]==country_name, last_updated_date].values[0]/confirmed_cases_country.loc[confirmed_cases_country["country"]==country_name, columns[-n_days-1]].values[0]<2):
                n_days+=1
            else:
                new_confirmed_cases_country_1.loc[new_confirmed_cases_country_1["country"]==country_name,"n_days_cases_double"]= n_days
                break
        except:
            new_confirmed_cases_country_1.loc[new_confirmed_cases_country_1["country"]==country_name,"n_days_cases_double"]= float("inf")
            

data= go.Bar(
                y = new_confirmed_cases_country_1.iloc[:50,:]['country'],
                x = new_confirmed_cases_country_1.iloc[:50,:]["n_days_cases_double"],
                name = country_name,
                marker = dict(color = '#46cdcf', opacity= 0.7,
                              line=dict(color='rgb(0,0,0)',width=1.5)),
                text= new_confirmed_cases_country_1["n_days_cases_double"],
                textposition='outside', hoverinfo= 'y+x',
                orientation= 'h'
                )
layout = dict(
    height = 1200, width= 1000,
    plot_bgcolor='rgba(240,240,240,0.8)',
    title = '<b>Number of days to double - Confirmed cases</b>', title_x=0.55, title_y= 0.95,
    xaxis= dict(title= '<b>Numbe of days</b>'), yaxis= dict(title= '', autorange="reversed"),
    # top, bottom, left and right margins
#     margin = dict(t = 0, b = 0, l = 0, r = 0),
    
    font = dict(color = 'rgba(0, 0, 0, 0.8)'),
#     paper_bgcolor = '#000000',
    showlegend=False,
    
    )
figure = dict(data = data, layout = layout)
iplot(figure)

Country-wise trend of deaths reported - Dropdown

In [29]:
n_countries= confirmed_cases_country.shape[0]
data = []


#Preparing data for World scatter plot
deaths_world= deaths_country.iloc[:,3:].sum(axis=0)
deaths_world_time= deaths_world.transpose().reset_index()
deaths_world_time.columns= ['Date', 'Number of deaths']
country_trace = go.Scatter(
                x = deaths_world_time['Date'],
                y = deaths_world_time["Number of deaths"],
                name = "World",
#                 marker = dict(color = '#46cdcf', opacity= 0.7, line=dict(color='rgb(0,0,0)',width=1.5)),
#                 text= '+'+ str (new_confirmed_cases_country_time["Number of new confirmed cases"]),
                text= "World"+ '<br><br>',
#                 textposition='outside',
                mode= 'lines+markers', hoverinfo= 'text+x+y'
                )

data.append(country_trace)


#preparing data for the scatter plot country-wise
for i in range(deaths_country_sorted.shape[0]):
    country_name= deaths_country_sorted.iloc[i,:]["country"]
    deaths_country_df= deaths_country.loc[deaths_country['country'] == country_name,:]
    deaths_country_time= deaths_country_df.transpose().reset_index().iloc[3:,:]
    deaths_country_time.columns= ['Date', 'Number of deaths']
#     deaths_country_df= deaths_country.loc[deaths_country['country'] == country_name,:]
#     active_cases_country_df= active_cases_country.loc[active_cases_country['country'] == country_name,:]
#     recovered_cases_country_df= recovered_cases_country.loc[recovered_cases_country['country'] == country_name,:]
   
    country_trace = go.Scatter(
                x = deaths_country_time['Date'],
                y = deaths_country_time["Number of deaths"],
                name = country_name,
                text= country_name+ '<br><br>',
#                 marker = dict(color = 'rgba(80, 26, 80, 0.8)', opacity= 1),
                mode= 'lines+markers', hoverinfo='text+x+y'
                )

    data.append(country_trace)

#designing dropdown menu for the layout of plot

list_updatemenus_buttons= []
title_text= '<b>Deaths trend </b>'
default_dict= dict(label = "All",
                 method = 'update',
                 args = [{'visible': [True]*(n_countries+1)},
                         {'title': title_text}])

list_updatemenus_buttons.append(default_dict)

title_text= '<b>Total deaths reported: {}<br>Deaths per million: {}<b>' \
                .format(table_stat_country.loc[table_stat_country["Country"]=="World", "Total deaths"].values[0], \
                       table_stat_country.loc[table_stat_country["Country"]=="World", "Total deaths per million"].values[0])

visible= [False]*(n_countries+1)
visible[0]= True
default_dict= dict(label = "World",
                 method = 'update',
                 args = [{'visible': visible},
                         {'title': title_text}])

list_updatemenus_buttons.append(default_dict)



for i in range(deaths_country_sorted.shape[0]):
    country_name= deaths_country_sorted.iloc[i,0]
    title_text= '<b>Total deaths reported: {}<br>Deaths per million: {}</b>' \
                .format(table_stat_country.loc[table_stat_country["Country"]==country_name, "Total deaths"].values[0], \
                       table_stat_country.loc[table_stat_country["Country"]==country_name, "Total deaths per million"].values[0])
    visible = [False] * (n_countries+1)
    visible[i+1] = True
    temp_dict = dict(label = country_name,
                 method = 'update',
                 args = [{'visible': visible},
                         {'title': title_text}, {'font':11}])
    list_updatemenus_buttons.append(temp_dict)
    
    
    
    # drop-down 2: select type of storm event to visualize
list_updatemenus = list( [dict(
         # for each button I specify which dictionaries of my data list I want to visualize. Remember I have 7 different
         # types of storms but I have 8 options: the first will show all of them, while from the second to the last option, only
         # one type at the time will be shown on the map
         buttons= list_updatemenus_buttons,
        type= 'dropdown',
        # direction where the drop-down expands when opened
        direction = 'down',
        # positional arguments
        x = -0.07,
        xanchor = 'right',
        y = 0.90,
        yanchor = 'bottom',
        # fonts and border
        bgcolor = '#EEEEEE',
        bordercolor = '#FFFFFF',
        font = dict(color = '#000000', size=14)
    )
])


#basic layout info for the plot
layout = dict(
    height = 600, width= 1200,
    plot_bgcolor='rgba(240,240,240,0.8)',
    title = '<b>Trend of deaths reported</b>', title_x=0.55, title_y= 0.9,
    xaxis= dict(title= '<b>Date</b>', tickangle= -45), yaxis= dict(title= ''),
    # top, bottom, left and right margins
#     margin = dict(t = 0, b = 0, l = 0, r = 0),
    
    font = dict(color = 'rgba(0, 0, 0, 0.8)'),
#     paper_bgcolor = '#000000',
#     showlegend=False,
    
    )


# list_updatemenus.append(list_updatemenus_1)
# assign the list of dictionaries to the layout dictionary
layout['updatemenus'] = list_updatemenus

figure = dict(data = data, layout = layout)
iplot(figure)

New deaths reported over time - Dropdown

In [30]:
data = []
n_countries= deaths_country.shape[0]
new_deaths_country= deaths_country_sorted.copy()
for i in range(4, deaths_country_sorted.shape[1]):
    new_deaths_country.iloc[:,i]= deaths_country_sorted.iloc[:,i]-deaths_country_sorted.iloc[:,i-1]


#adding trace for World data for default dropdown selection
new_deaths_world= new_deaths_country.iloc[:,3:].sum(axis=0)
new_deaths_country_time= new_deaths_world.transpose().reset_index().iloc[3:,:]
new_deaths_country_time.columns= ['Date', 'Number of new deaths']
country_trace = go.Scatter(
                x = new_deaths_country_time['Date'],
                y = new_deaths_country_time["Number of new deaths"],
                name = "World",
#                 marker = dict(color = '#46cdcf', opacity= 0.7,line=dict(color='rgb(0,0,0)',width=1.5)),
#                 text= '+'+ str (new_deaths_country_time["Number of new deaths"]),
#                 textposition='outside', 
                text= "World"+ '<br><br>',
                mode= 'lines+markers', hoverinfo= 'text+x+y'
                )

data.append(country_trace)
    

#preparing data for the scatter plot country-wise
for i in range(deaths_country_sorted.shape[0]):
    country_name= deaths_country_sorted.iloc[i,:]["country"]
    new_deaths_country_df= new_deaths_country.loc[new_deaths_country['country'] == country_name,:]
    new_deaths_country_time= new_deaths_country_df.transpose().reset_index().iloc[3:,:]
    new_deaths_country_time.columns= ['Date', 'Number of new deaths']
#     deaths_country_df= deaths_country.loc[deaths_country['country'] == country_name,:]
#     active_cases_country_df= active_cases_country.loc[active_cases_country['country'] == country_name,:]
#     recovered_cases_country_df= recovered_cases_country.loc[recovered_cases_country['country'] == country_name,:]
   
    country_trace = go.Scatter(
                x = new_deaths_country_time['Date'],
                y = new_deaths_country_time["Number of new deaths"],
                name = country_name,
#                 marker = dict(color = '#46cdcf', opacity= 0.7, line=dict(color='rgb(0,0,0)',width=1.5)),
#                 text= '+'+ str (new_deaths_country_time["Number of new deaths"]),
#                 textposition='outside', 
                text= country_name+ '<br><br>',
                mode= 'lines+markers', hoverinfo= 'text+x+y'
                )

    data.append(country_trace)


#designing dropdown menu for the layout of plot

list_updatemenus_buttons= []
title_text= '<b>Trend of new deaths reported </b>'

visible = [True] * (n_countries+1)
default_dict= dict(label = "All",
                 method = 'update',
                 args = [{'visible': visible},
                         {'title': title_text}])

list_updatemenus_buttons.append(default_dict)

visible = [False] * (n_countries+1)
visible[0]= True
default_dict= dict(label = "World",
                 method = 'update',
                 args = [{'visible': visible},
                         {'title': title_text}])


list_updatemenus_buttons.append(default_dict)
for i in range(deaths_country_sorted.shape[0]):
    country_name= deaths_country_sorted.iloc[i,:]["country"]
    title_text= '<b>Trend of new deaths reported</b>'
    
    visible = [False] * (n_countries+1)
    visible[i+1] = True
    temp_dict = dict(label = country_name,
                 method = 'update',
                 args = [{'visible': visible},
                         {'title': title_text}, {'font':11}])
    list_updatemenus_buttons.append(temp_dict)
    
    
    
    # drop-down 2: select type of storm event to visualize
list_updatemenus = list( [dict(
         # for each button I specify which dictionaries of my data list I want to visualize. Remember I have 7 different
         # types of storms but I have 8 options: the first will show all of them, while from the second to the last option, only
         # one type at the time will be shown on the map
         buttons= list_updatemenus_buttons,
        type= 'dropdown',
        # direction where the drop-down expands when opened
        direction = 'down',
        # positional arguments
        x = -0.09,
        xanchor = 'right',
        y = 0.90,
        yanchor = 'bottom',
        # fonts and border
        bgcolor = '#EEEEEE',
        bordercolor = '#FFFFFF',
        font = dict(color = '#000000', size=14)
    )
])


#basic layout info for the plot
layout = dict(
    height = 600, width= 1200,
    plot_bgcolor='rgba(240,240,240,0.8)',
    title = '<b>Trend of new deaths reported</b>', title_x=0.55, title_y= 0.9,
    xaxis= dict(title= '<b>Date</b>', tickangle= -45), yaxis= dict(title= ''),
    # top, bottom, left and right margins
#     margin = dict(t = 0, b = 0, l = 0, r = 0),
    
    font = dict(color = 'rgba(0, 0, 0, 0.8)'),
#     paper_bgcolor = '#000000',
#     showlegend=False,
    
    )


# list_updatemenus.append(list_updatemenus_1)
# assign the list of dictionaries to the layout dictionary
layout['updatemenus'] = list_updatemenus

figure = dict(data = data, layout = layout)
iplot(figure)

Number of days to double - Deaths reported

In [31]:
columns= deaths_country.columns
new_deaths_country_1= new_deaths_country.copy()
new_deaths_country_1["n_days_deaths_double"]=0
for i in range(deaths_country_sorted.shape[0]):
    country_name= deaths_country_sorted.iloc[i,:]["country"]
#     print(country_name)
    n_days=1
    while(True):
        try:
            if(deaths_country.loc[deaths_country["country"]==country_name, last_updated_date].values[0]/deaths_country.loc[deaths_country["country"]==country_name, columns[-n_days-1]].values[0]<2):
                n_days+=1
            else:
                new_deaths_country_1.loc[new_deaths_country_1["country"]==country_name,"n_days_deaths_double"]= n_days
                break
        except:
            new_deaths_country_1.loc[new_deaths_country_1["country"]==country_name,"n_days_deaths_double"]= float("inf")
            

data= go.Bar(
                y = new_deaths_country_1.iloc[:50,:]['country'],
                x = new_deaths_country_1.iloc[:50,:]["n_days_deaths_double"],
                name = country_name,
                marker = dict(color = '#46cdcf', opacity= 0.7,
                              line=dict(color='rgb(0,0,0)',width=1.5)),
                text= new_deaths_country_1["n_days_deaths_double"],
                textposition='outside', hoverinfo= 'x+y',
                orientation= 'h'
                )
layout = dict(
    height = 1200, width= 1000,
    plot_bgcolor='rgba(240,240,240,0.8)',
    title = '<b>Number of days to double - Deaths reported</b>', title_x=0.55, title_y= 0.95,
    xaxis= dict(title= '<b>Numbe of days</b>'), yaxis= dict(title= '', autorange="reversed"),
    # top, bottom, left and right margins
#     margin = dict(t = 0, b = 0, l = 0, r = 0),
    
    font = dict(color = 'rgba(0, 0, 0, 0.8)'),
#     paper_bgcolor = '#000000',
    showlegend=False,
    
    )
figure = dict(data = data, layout = layout)
iplot(figure)        

Country-wise trend of tests performed - Dropdown

In [32]:
data = []


#preparing data for the scatter plot country-wise
for i in range(country_list_test.shape[0]):
    country_name= country_list_test[i]
    test_df_grp_country= test_df_grp.loc[test_df_grp['country'] == country_name,:]
#     test_df_grp_country= test_df_grp_country.dropna(how="any", subset=["Cumulative total"])
    
#     test_df_
#     deaths_country_time= deaths_country_df.transpose().reset_index().iloc[3:,:]
#     deaths_country_time.columns= ['Date', 'Number of deaths']
#     deaths_country_df= deaths_country.loc[deaths_country['country'] == country_name,:]
#     active_cases_country_df= active_cases_country.loc[active_cases_country['country'] == country_name,:]
#     recovered_cases_country_df= recovered_cases_country.loc[recovered_cases_country['country'] == country_name,:]
   
    country_trace = go.Scatter(
                x = test_df_grp_country['Date'],
                y = test_df_grp_country["Total tests"],
                name = country_name,
                text= country_name+ '<br><br>',
#                 marker = dict(color = 'rgba(80, 26, 80, 0.8)', opacity= 1),
                mode= 'lines+markers', hoverinfo= "text+x+y"
                )

    data.append(country_trace)


#annotaions for the layout of plot
annotations = [dict(
  
              # text I want to display. I used <br> to break it into two lines
              text = 'All US storm events that caused more than $50k of economic damage,<br> from 2000 until today', 
              
              # font and border characteristics
              font = dict(color = '#FFFFFF', size = 14), borderpad = 10, 
              
              # positional arguments
              x = 0.05, y = 0.05, xref = 'paper', yref = 'paper', align = 'left', 
              
              # don't show arrow and set background color
              showarrow = False, bgcolor = 'black'
              )]

# assigning the annotations to the layout
# layout['annotations'] = annotations

#designing dropdown menu for the layout of plot

list_updatemenus_buttons= []
title_text= '<b>Trend of number of tests performed</b>'
default_dict= dict(label = "All",
                 method = 'update',
                 args = [{'visible': [True]*n_countries_test},
                         {'title': title_text}])

list_updatemenus_buttons.append(default_dict)

for i in range(country_list_test.shape[0]):
    country_name= country_list_test[i]
    title_text= '<b>Tests per million: {}</b>' \
                .format(test_df_grp.loc[test_df_grp["country"]==country_name, "Total tests per million"].iloc[-1])
    visible = [False] * n_countries_test
    visible[i] = True
    temp_dict = dict(label = country_name,
                 method = 'update',
                 args = [{'visible': visible},
                         {'title': title_text}, {'font':11}])
    list_updatemenus_buttons.append(temp_dict)
    
    
    
    # drop-down 2: select type of storm event to visualize
list_updatemenus = list( [dict(
         # for each button I specify which dictionaries of my data list I want to visualize. Remember I have 7 different
         # types of storms but I have 8 options: the first will show all of them, while from the second to the last option, only
         # one type at the time will be shown on the map
         buttons= list_updatemenus_buttons,
        type= 'dropdown',
        # direction where the drop-down expands when opened
        direction = 'down',
        # positional arguments
        x = -0.07,
        xanchor = 'right',
        y = 0.90,
        yanchor = 'bottom',
        # fonts and border
        bgcolor = '#EEEEEE',
        bordercolor = '#FFFFFF',
        font = dict(color = '#000000', size=14)
    )
])


#basic layout info for the plot
layout = dict(
    height = 600, width= 1000,
    plot_bgcolor='rgba(240,240,240,0.8)', title_x=0.55, title_y= 0.90,
    title = '<b>Trend of number of tests performed</b>',
    xaxis= dict(title= '<b>Date</b>', tickangle= -45), yaxis= dict(title= ''),
    # top, bottom, left and right margins
#     margin = dict(t = 0, b = 0, l = 0, r = 0),
    
    font = dict(color = 'rgba(0, 0, 0, 0.8)'),
#     paper_bgcolor = '#000000',
#     showlegend=False,
    
    )


# list_updatemenus.append(list_updatemenus_1)
# assign the list of dictionaries to the layout dictionary
layout['updatemenus'] = list_updatemenus

figure = dict(data = data, layout = layout)
iplot(figure)

New tests performed over time - Dropdown

In [33]:
data = []


#preparing data for the scatter plot country-wise
for i in range(country_list_test.shape[0]):
    country_name= country_list_test[i]
    test_df_grp_country= test_df_grp.loc[test_df_grp['country'] == country_name,:]
#     test_df_grp_country= test_df_grp_country.dropna(how="any", subset=["Cumulative total"])
    
#     test_df_
#     deaths_country_time= deaths_country_df.transpose().reset_index().iloc[3:,:]
#     deaths_country_time.columns= ['Date', 'Number of deaths']
#     deaths_country_df= deaths_country.loc[deaths_country['country'] == country_name,:]
#     active_cases_country_df= active_cases_country.loc[active_cases_country['country'] == country_name,:]
#     recovered_cases_country_df= recovered_cases_country.loc[recovered_cases_country['country'] == country_name,:]
   
    country_trace = go.Scatter(
                x = test_df_grp_country['Date'],
                y = test_df_grp_country["New tests"],
                name = country_name,
                text= country_name+ '<br><br>',
#                 marker = dict(color = 'rgba(80, 26, 80, 0.8)', opacity= 1),
                mode= 'lines+markers', hoverinfo= "text+x+y"
                )

    data.append(country_trace)

#designing dropdown menu for the layout of plot

list_updatemenus_buttons= []
title_text= '<b>Trend of new tests performed</b>'
default_dict= dict(label = "All",
                 method = 'update',
                 args = [{'visible': [True]*n_countries_test},
                         {'title': title_text}])

list_updatemenus_buttons.append(default_dict)
for i in range(country_list_test.shape[0]):
    country_name= country_list_test[i]
    title_text= '<b>Trend of new tests performed</b>'
    visible = [False] * n_countries_test
    visible[i] = True
    temp_dict = dict(label = country_name,
                 method = 'update',
                 args = [{'visible': visible},
                         {'title': title_text}, {'font':11}])
    list_updatemenus_buttons.append(temp_dict)
    
    
    
    # drop-down 2: select type of storm event to visualize
list_updatemenus = list( [dict(
         # for each button I specify which dictionaries of my data list I want to visualize. Remember I have 7 different
         # types of storms but I have 8 options: the first will show all of them, while from the second to the last option, only
         # one type at the time will be shown on the map
         buttons= list_updatemenus_buttons,
        type= 'dropdown',
        # direction where the drop-down expands when opened
        direction = 'down',
        # positional arguments
        x = -0.07,
        xanchor = 'right',
        y = 0.90,
        yanchor = 'bottom',
        # fonts and border
        bgcolor = '#EEEEEE',
        bordercolor = '#FFFFFF',
        font = dict(color = '#000000', size=14)
    )
])


#basic layout info for the plot
layout = dict(
    height = 600, width= 1000,
    plot_bgcolor='rgba(240,240,240,0.8)', title_x=0.55, title_y= 0.9,
    title = '<b>Trend of new tests performed</b>'.format(last_updated_date),
    xaxis= dict(title= '<b>Date</b>', tickangle= -45), yaxis= dict(title= ''),
    # top, bottom, left and right margins
#     margin = dict(t = 0, b = 0, l = 0, r = 0),
    
    font = dict(color = 'rgba(0, 0, 0, 0.8)'),
#     paper_bgcolor = '#000000',
#     showlegend=False,
    
    )


# list_updatemenus.append(list_updatemenus_1)
# assign the list of dictionaries to the layout dictionary
layout['updatemenus'] = list_updatemenus

figure = dict(data = data, layout = layout)
iplot(figure)

Number of tests per million

In [34]:
test_df_grp_country= test_df_grp.groupby(by="country")["Total tests per million"].max()
test_df_grp_country= test_df_grp_country.reset_index()
test_df_grp_country["Total tests per million"]= test_df_grp_country["Total tests per million"]
test_df_grp_country_sort= test_df_grp_country.sort_values(by="Total tests per million", ascending= False)
data= go.Bar(
                y = test_df_grp_country_sort.iloc[:80,:]['country'],
                x = test_df_grp_country_sort.iloc[:80,:]["Total tests per million"],
                name = country_name,
                marker = dict(color = 'rgba(100, 216, 210, 0.8)', opacity= 0.9,
                              line=dict(color='rgb(0,0,0)',width=1.5)),
                text= test_df_grp_country_sort.iloc[:80,:]["Total tests per million"],
                textposition='outside', hoverinfo= 'x+y',
                orientation= 'h'
                )
layout = dict(
    height = 1800, width= 1500,
    plot_bgcolor='rgba(240,240,240,0.8)',
    title = '<b>Number of tests per million</b>', title_x=0.37, title_y= 0.96,
    xaxis= dict(title= ''), yaxis= dict(title= '', autorange="reversed"),
    # top, bottom, left and right margins
#     margin = dict(t = 0, b = 0, l = 0, r = 0),
    
    font = dict(color = 'rgba(0, 0, 0, 0.8)'),
#     paper_bgcolor = '#000000',
    showlegend=False,
    
    )
figure = dict(data = data, layout = layout)
iplot(figure)